我正在开发一款Android应用,它使用Cloud Endpoints(Python)和Google帐户作为身份验证。我已经完成了很好的设置,它在API Explorer中运行良好,我已经设法生成Gradle客户端库并在Android Studio中使用它,...
我遇到的问题是这样的:当使用Audience从Android进行身份验证时(如指南所说),我无法完成请求。 oauth Bearer令牌引擎接收的是巨大的(超过800个字符),我的日志显示此消息:Oauth框架用户与oauth令牌用户不匹配。我使用的观众是正确的,当我在观众中输入拼写错误时,Google拒绝首先给我一个令牌。如果我使用范围进行身份验证(仅使用.usingOAuth2替换GoogleAccountCredential.usingAudience),一切正常,我会得到一个“常规”,更短的令牌。
在使用Audience时,我做错了什么导致巨大的代币,为什么我首先应该使用Audience而不是范围?
服务器代码:
API = endpoints.api(
name='apiDaycare',
version='v1',
description='API for Appy Daycare',
audiences=['--WEB CLIENT ID HERE--'],
scopes=[endpoints.EMAIL_SCOPE, 'https://www.googleapis.com/auth/plus.login'],
allowed_client_ids=[endpoints.API_EXPLORER_CLIENT_ID, '--Android app client ID here--', '--web client ID here'],
canonical_name='Appy Daycare API',
owner_name='Ambroos Vaes',
title='Appy Daycare API',
auth_level=AUTH_LEVEL.REQUIRED)
应用代码:
private void LoginTest(String accountName) {
GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(this, Constants.AUDIENCE);
credential.setSelectedAccountName(accountName);
AppyDaycareAPI.Builder builder = new AppyDaycareAPI.Builder(
AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), credential
);
builder.setApplicationName("Appy Daycare Android");
AppyDaycareAPI service = builder.build();
TempTestTask task = new TempTestTask(this, service, credential);
task.execute();
}
private class TempTestTask extends AsyncTask<Void, Void, ModelsUsersAccountMessage> {
private final Context context;
private final AppyDaycareAPI service;
private final GoogleAccountCredential credential;
public TempTestTask(Context context, AppyDaycareAPI service, GoogleAccountCredential credential) {
this.context = context;
this.credential = credential;
this.service = service;
}
protected ModelsUsersAccountMessage doInBackground(Void... unused) {
Log.d("Account API test", "Token:");
Log.d("Account API test", credential.getToken());
ModelsUsersAccountMessage result = null;
result = service.account().echo().execute();
return result;
}
@Override
protected void onPostExecute(ModelsUsersAccountMessage modelsUsersAccountMessage) {
Log.d("Account API test", modelsUsersAccountMessage.toPrettyString());
}
}