我正在開發一個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
我想!