我在我的 Android 应用程序中使用 Google+ 登录,我想将身份验证令牌发送到我的网络服务器。我点击链接 [ https://developers.google.com/identity/sign-in/android/backend-auth] [1] 并使用以下代码生成 idToken。
private class GetIdTokenTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String accountName = Plus.AccountApi.getAccountName(mGoogleApiClient);
Account account = new Account(accountName, GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
String scopes = "audience:server:client_id:" + Constants.SERVER_CLIENT_ID; // Not the app's client ID.
String idToken = "";
try {
idToken = GoogleAuthUtil.getToken(getApplicationContext(), account, scopes);
} catch (IOException e) {
Log.e(TAG, "Error retrieving ID token.", e);
} catch (GoogleAuthException e) {
Log.e(TAG, "Error retrieving ID token.", e);
}
return idToken;
}
@Override
protected void onPostExecute(String result) {
authToken=result;
Log.i(TAG, "ID token: " + result);
}
}
我得到一个像这样的 id 令牌:
eyJhbGciOiJSUzI1NiIsImtpZCI6IxxxxxNThkODVjODU3hiMjI3MjBhM2E1NWxxxxzM0NDcifQ.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwic3ViIjoiMTEyMzA2OTkwMjA1NDYyMjkzMDExIiwiYXpwIjoiNDU5NTM4MTI1OTIzLXUzNTEzNGZ0aTZyOWJnNGM4MDFjZW82MzVhNWVoNTZzLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiZW1haWwiOiJsZWppbmtyQGdtYWlsLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjp0cnVlLCJhdWQiOiI0NTk1MzgxMjU5MjMtNDZlN3Q3YzNhbmQ3M2VqcThyZWZvZTFzYTIzdm9wOHEuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJpYXQiOjE0MzI2MzI5ODUsImV4cCI6MTQzMjYzNjU4NX0.vCT8lanz5Pa2_m_AQ4tZHaME_y_ffa5zqA5_O3u7ZhuOQTmK8xXUh7M-Q_R67KosGPX-ikr_7Tz57OiXNIyKtNBOFKssssxxE0v7wEJvFhVgLSpbuD
这是 idToken 的真实格式吗?
但是我的后端服务器身份验证令牌需要这样的格式(其他一些人在做后端)
ya29.QwGqNA9LsssggddjKZxxRD74HT_viU0wHxoDieWbsIJG3R0xvW3bDij4jzxxxxxP6wPSi7w1bysA
后端编码人员建议我使用 urlshortener 和 userinfo.email 之类的东西来获取这种令牌。我怎样才能获得这种类型的令牌?我正在使用 GoogleApiClient 与 google plus 连接并在以下代码中驱动 api:
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addApi(Plus.API)
.addScope(new Scope("profile"))
.addScope(Drive.SCOPE_APPFOLDER) // required for App Folder sample
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
findViewById(R.id.sign_in_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mGoogleApiClient.connect();
}
});
如何以我上面描述的缩短格式获取身份验证令牌?