2016-01-21 33 views
3

我正在開發一個Android應用程序,該應用程序需要一個用於Google帳戶登錄的Google OAuth 2.0 API代碼,該代碼將用於同一項目下的Web應用程序。從Android上的Google Cross-Client身份驗證獲取代碼

我沒有得到該代碼,而是得到AccessToken

我跟着這個docs by Google 利用他們AsyncTask CALSS

public class GetUsernameTask extends AsyncTask<Void, Void, Void> { 
    Activity mActivity; 
    String mScope; 
    String mEmail; 
    private String token; 

    public GetUsernameTask(Activity activity, String name, String scope) { 
      this.mActivity = activity; 
      this.mScope = scope; 
      this.mEmail = name; 
     } 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      DialogsUtil.showProgressDialog(mActivity, DialogsUtil.PROGRESS_SIGNIN); 
     } 

     @Override 
     protected Void doInBackground(Void... params) { 
      try { 
       token = fetchToken(); 
       if (token != null) { 
        // My method to make a backend call using the `token` 
        loginWithGoogleToken(token); 
       } 
      } catch (IOException e) { 
      } 
      DialogsUtil.dismissProgressDialog(); 
      return null; 
     } 

     protected String fetchToken() throws IOException { 
      try { 
       return GoogleAuthUtil.getToken(mActivity, mEmail, mScope); 
      } catch (UserRecoverableAuthException userRecoverableException) { 
      } catch (GoogleAuthException fatalException) { 
      } 
      return null; 
     } 
    } 

我展示了AccountPicker點擊一個按鈕爲:

String[] accountTypes = new String[]{"com.google"}; 
Intent intent = AccountPicker.newChooseAccountIntent(null, null,accountTypes, false, null, null, null, null); 
startActivityForResult(intent, RC_SIGN_IN); 

然後在onActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

     if (requestCode == RC_SIGN_IN) { 
     // Initialize scope 
     String clientID = context.getResources().getString(R.string.server_client_id); 
     String audienceScope = "audience:server:client_id:" + clientID; 
     String email = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME); 
     new GetUsernameTask(activity, email, audienceScope).execute(); 
    }} 

運用範圍audience:server:client_id: + clientID使GoogleAuthUtil.getToken()返回AccessToken這不是我想要的。

使用範圍作爲String.format("oauth2:server:client_id:%s:api_scope:https://www.googleapis.com/auth/userinfo.profile", clientID);使GoogleAuthUtil.getToken()返回一個null不是預期的Code我想!

回答

2

服務器所使用的短代碼被稱爲serverAuthCode,您可以通過以下步驟得到它:

首先建立SDK與server-client-idscopes

String serverClientId = getString(R.string.server_client_id); 
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
     .requestScopes(new Scope(Scopes.PLUS_ME)) 
     .requestServerAuthCode(serverClientId, false) 
     .build(); 

// Build GoogleAPIClient with the Google Sign-In API and the above options. 
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this) 
     .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */) 
     .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
     .build(); 


Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
startActivityForResult(signInIntent, RC_SIGN_IN); 

和活動結果

GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); 
if (result.isSuccess()) { 
    GoogleSignInAccount acct = result.getSignInAccount(); 
    String idToken = acct.getServerAuthCode(); 
    Log.e(TAG, "ServerToken : " + acct.getServerAuthCode()); 

    // send token to your backend 
    loginWithGoogleToken(idToken); 
} else { 
    Log.e(TAG, "Error getting token"); 
}