gpt4 book ai didi

android - 从 Android 上的 Google 跨客户端身份验证获取代码

转载 作者:行者123 更新时间:2023-11-29 00:04:30 26 4
gpt4 key购买 nike

我正在开发一款 Android 应用,该应用需要用于 Google 帐户登录的 Google OAuth 2.0 API 代码,同一项目下的网络应用将使用该代码。

我未能获得该代码,而是获得了一个 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() 返回一个 AccessToken 这不是我想要的。

使用范围作为 String.format("oauth2:server:client_id:%s:api_scope:https://www.googleapis.com/auth/userinfo.profile", clientID); 使得GoogleAuthUtil.getToken()返回一个 null 而不是我想要的预期 Code!

最佳答案

服务器使用的短代码称为serverAuthCode,您可以通过以下步骤获取它:

首先使用server-client-idscopes 设置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 - 从 Android 上的 Google 跨客户端身份验证获取代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34920381/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com