Google Calendar API returning "Invalid Credentials" (401) -
i having problems authenticating google calendar api v3 using java client library (google-api-client version 1.13.2-beta, google-api-services-calendar version v3-rev26-1.13.2-beta).
we have marketplace app, , installing app should give required credentials reading (and writing) google calendar events.
we using 2-legged oauth 1.0, hmac, meaning no tokens necessary. trying do, calendar instead of tasks: https://developers.google.com/google-apps/help/articles/2lo-in-tasks-for-marketplace
this error i'm getting google:
com.google.api.client.googleapis.json.googlejsonresponseexception: 401 unauthorized { "code" : 401, "errors" : [ { "domain" : "global", "location" : "authorization", "locationtype" : "header", "message" : "invalid credentials", "reason" : "autherror" } ], "message" : "invalid credentials" }
we have customers don't use marketplace app, have given our domain appropriate permissions hand, , works fine them. customers marketplace app have problem (using different consumerkey , consumersecret), , leads me believe there nothing wrong code, overlooked somewhere in google setup..
the permissions marketplace app fine.
i have followed every guide find, appropriate our setup (not plenty of them out there though), , far can see i'm doing right thing.
the api key seems correctly set (access calendar api granted). mentioned, works our customers not coming marketplace app, , using same api key.
anyway, here's failing junit test reproduce error:
import java.io.ioexception; import java.util.list; import org.junit.test; import com.google.api.client.auth.oauth.oauthhmacsigner; import com.google.api.client.auth.oauth.oauthparameters; import com.google.api.client.http.javanet.nethttptransport; import com.google.api.client.json.jackson.jacksonfactory; import com.google.api.services.calendar.calendar; import com.google.api.services.calendar.calendarrequest; import com.google.api.services.calendar.calendarrequestinitializer; import com.google.api.services.calendar.model.calendarlistentry; import junit.framework.assert; public class googleoauthtest { @test public void test() { final string api_key = "..."; final string consumer_key = "..."; final string consumer_key_secret = "..."; final string google_user = "me@mydomain.com"; // 2-lo authorization section oauthhmacsigner signer = new oauthhmacsigner(); signer.clientsharedsecret = consumer_key_secret; final oauthparameters oauthparameters = new oauthparameters(); oauthparameters.version = "1"; oauthparameters.consumerkey = consumer_key; oauthparameters.signer = signer; calendar service = new calendar.builder(new nethttptransport(), new jacksonfactory(), oauthparameters) .setapplicationname("myapp") .setcalendarrequestinitializer(new calendarrequestinitializer() { @override protected void initializecalendarrequest(calendarrequest<?> request) throws ioexception { request.setkey(api_key); } }).build(); try { com.google.api.services.calendar.calendar.calendarlist.list list = service.calendarlist().list(); list.getunknownkeys().put("xoauth_requestor_id", google_user); // exception! list<calendarlistentry> calendarlist = list.execute().getitems(); } catch (ioexception e) { assert.fail("test failed: " + e.getmessage()); } } }
what doing wrong? there wrong code, or there might have missed in google setup?
not sure api key is, have similar code except request initializer :
calendarrequestinitializer initializer = new calendarrequestinitializer() { @override protected void initializecalendarrequest(calendarrequest<?> calendarrequest) throws ioexception { arraymap<string, object> customkeys = new arraymap<string, object>(); customkeys.add("xoauth_requestor_id", email_of_the_user); calendarrequest.setunknownkeys(customkeys); } };
this works me... hope helps;
update use api key when clicking "register additional apis" link in marketplace -> vendor profile -> app.
Comments
Post a Comment