Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get access token from google plus Android

Can anyone tell me what am I doing wrong? I need to get the access token from Google Plus..

I put this in my onConnected() method but I am not getting the access token, instead I am getting error...

Code:

try {
        String token = GoogleAuthUtil.getToken(this, mPlusClient.getAccountName() + "", "oauth2:" + Scopes.PLUS_PROFILE + 
                                "https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email");
        Log.d("AccessToken", token);
    } catch (UserRecoverableAuthException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (GoogleAuthException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Error:

08-07 10:10:24.199: E/GoogleAuthUtil(17203): Calling this from your main thread can lead to deadlock and/or ANRs

Can anyone tell me what would be the correct way to get the Google Plus access token from the user?

like image 957
Karlis Avatar asked Aug 07 '13 07:08

Karlis


2 Answers

You need to put the request for a token in a background thread. I've posted some example code showing how to do it in this question:

"Calling this from your main thread can lead to deadlock and/or ANRs while getting accesToken" from GoogleAuthUtil(Google Plus integration in Android)

like image 131
Lee Avatar answered Nov 15 '22 04:11

Lee


You can access token in onConnected() method. add this code onConnected() methods scope.

final String SCOPES = "https://www.googleapis.com/auth/userinfo.profile";
         new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
               String ace = "";
               try {
                  ace = GoogleAuthUtil.getToken(getApplicationContext(),
                                                Plus.AccountApi.getAccountName(mGoogleApiClient),
                                                "oauth2:" + SCOPES);
               }
               catch (IOException e) {
                  e.printStackTrace();
               }
               catch (GoogleAuthException e) {
                  e.printStackTrace();
               }
               Log.i("", "mustafa olll " + ace);
               return null;
            }
         }.execute();
like image 4
msevgi Avatar answered Nov 15 '22 06:11

msevgi