sorry, corrected here:
*try*{
calendarList = CalendarService.calendarList().list().execute();
}*catch* (Exception e) {
e.printStackTrace();
accountManager.invalidateAuthToken(account.type, myToken);
//start all over
}
On Thursday, November 22, 2012 10:30:41 AM UTC+1, Christopher Masser wrote:
>
>
> I'm using the AccountManager getAuthToken(..) method to retrieve a token
> for Google Calendar sync.
>
> While sync works in most cases, several users report the following error
> when trying to get the calendarList of the Google calendar API:
>
>
> 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"}
> Their native Android calendar app continues to sync normally.i.e., they do
> have internet connection and they do have registered accounts on their
> device.Here is what I do:
>
> AccountManager accountManager = AccountManager.get(this);Account[] accounts =
> accountManager.getAccountsByType("com.google");...//for each
> account:...accountManager.invalidateAuthToken(account.type,
> null);accountManager.getAuthToken(account,
> "oauth2:https://www.googleapis.com/auth/calendar", true, new
> AccountManagerCallback<Bundle>(){ public void
> run(AccountManagerFuture<Bundle> future) { try{
> Bundle bundle = future.getResult();
> if(bundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
> String token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
> AfterGetToken(token); }else {
> if(bundle.containsKey(AccountManager.KEY_INTENT)) {
> Intent intent = bundle.getParcelable(AccountManager.KEY_INTENT);
> intent.setFlags(intent.getFlags() &
> ~Intent.FLAG_ACTIVITY_NEW_TASK);
> activity.startActivityForResult(intent, REQUEST_AUTHENTICATE);
> //->start all over in onActivityResult }
> } }catch(Exception e){
> e.printStackTrace(); invalidateToken(account, null);
> //->start again } } }, null);
>
>
>
> private void AfterGetToken(String myToken){
>
>
> GoogleCredential credential = new
> GoogleCredential().setAccessToken(myToken);
>
>
> CalendarService = new
>
> com.google.api.services.calendar.Calendar.Builder(transport,
>
> new JacksonFactory(), null)
>
> .setApplicationName(MY_APP_NAME)
>
> .setHttpRequestInitializer(credential)
>
> .setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
>
> public void initialize(JsonHttpRequest request) throws IOException {
>
> com.google.api.services.calendar.CalendarRequest calendarRequest =
> (CalendarRequest) request;
>
> calendarRequest.setKey(MY_API_KEY);
>
> calendarRequest.setOauthToken(credential.getAccessToken());
>
> }
>
> }).build();
>
>
> CalendarList calendarList = null;
>
> * try*{
>
> calendarList = CalendarService.calendarList().list().execute();
>
> }*catch* (Exception e) {
>
> e.printStackTrace();
>
> }
>
> ....//do stuff with calendarList
>
> }
>
>
>
> //These are my imports:
>
> *import* android.accounts.Account;
>
> *import* android.accounts.AccountManager;
>
> *import* com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
>
> *import* com.google.api.client.http.HttpTransport;
>
> *import* com.google.api.client.http.javanet.NetHttpTransport;
>
> *import* com.google.api.client.http.json.JsonHttpRequest;
>
> *import* com.google.api.client.http.json.JsonHttpRequestInitializer;
>
> *import* com.google.api.client.json.jackson.JacksonFactory;
>
> *import* com.google.api.services.calendar.CalendarRequest;
>
> *import* com.google.api.services.calendar.model.*CalendarList*;
>
>
>
> Thanks for your help!
>
> Christopher
>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en