在 Android 上从 Google 跨客户端身份验证获取代码
Get CODE from Google Cross-Client Authentication on Android
我正在开发一个 Android 应用程序,它需要 Google 帐户登录的 Google OAuth 2.0 API
代码,该代码将由同一项目下的 Web 应用程序使用。
我未能获得该代码,我得到的是 AccessToken
。
我关注了这个docs by Google
使用他们的 AsyncTask
类
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()
return 成为我不想要的 AccessToken
。
将范围用作 String.format("oauth2:server:client_id:%s:api_scope:https://www.googleapis.com/auth/userinfo.profile", clientID);
会使 GoogleAuthUtil.getToken()
return 成为 null
而不是我想要的 Code
!
服务器使用的短代码称为serverAuthCode
,您可以通过以下步骤获取它:
首先使用 server-client-id
和 scopes
设置 sdk
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);
以及 activity 结果
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");
}
我正在开发一个 Android 应用程序,它需要 Google 帐户登录的 Google OAuth 2.0 API
代码,该代码将由同一项目下的 Web 应用程序使用。
我未能获得该代码,我得到的是 AccessToken
。
我关注了这个docs by Google
使用他们的 AsyncTask
类
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()
return 成为我不想要的 AccessToken
。
将范围用作 String.format("oauth2:server:client_id:%s:api_scope:https://www.googleapis.com/auth/userinfo.profile", clientID);
会使 GoogleAuthUtil.getToken()
return 成为 null
而不是我想要的 Code
!
服务器使用的短代码称为serverAuthCode
,您可以通过以下步骤获取它:
首先使用 server-client-id
和 scopes
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);
以及 activity 结果
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");
}