我正在开发一个使用GAE的Android应用程序,我正在尝试进行经过身份验证的调用。我有一个问题,使一切正常。当我检查授权函数(User)中的第一个参数时,它始终为null。 这是我的android代码:
settings = getSharedPreferences("TicTacToeSample", 0);
credential = GoogleAccountCredential.usingAudience(this,
"server:client_id:504148155997.apps.googleusercontent.com");
credential.setSelectedAccountName(accountName);
if (credential.getSelectedAccountName() == null) {
startActivityForResult(credential.newChooseAccountIntent(), 2);
} else {
Toast.makeText(getApplicationContext(),
credential.getSelectedAccountName(), 10000).show();
MessageEndpoint.Builder endpointBuilder = new MessageEndpoint.Builder(
AndroidHttp.newCompatibleTransport(), new GsonFactory(),
credential);
messageEndpoint = CloudEndpointUtils.updateBuilder(endpointBuilder)
.build();
}
private void setAccountName(String accountName) {
SharedPreferences.Editor editor = settings.edit();
editor.putString("pref", accountName);
editor.commit();
credential.setSelectedAccountName(accountName);
this.accountName = accountName;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 2:
if (data != null && data.getExtras() != null) {
String accountName = data.getExtras().getString(
AccountManager.KEY_ACCOUNT_NAME);
if (accountName != null) {
setAccountName(accountName);
SharedPreferences.Editor editor = settings.edit();
editor.putString("pref", accountName);
editor.commit();
// User is authorized.
Toast.makeText(getApplicationContext(),
credential.getSelectedAccountName(), 10000).show();
credential.setSelectedAccountName(accountName);
MessageEndpoint.Builder endpointBuilder = new MessageEndpoint.Builder(
AndroidHttp.newCompatibleTransport(),
new GsonFactory(), credential);
messageEndpoint = CloudEndpointUtils.updateBuilder(
endpointBuilder).build();
}
}
break;
}
}
这是我的GAE:
@Api(name = "deviceinfoendpoint", clientIds = { Ids.WEB_CLIENT_ID,
Ids.ANDROID_CLIENT_ID }, audiences = { Ids.ANDROID_AUDIENCE }, namespace = @ApiNamespace(ownerDomain = "safeandroid.com", ownerName = "safeandroid.com", packagePath = "mjmobilesolutions"))
public class DeviceInfoEndpoint {
...
@ApiMethod(name = "insertDeviceInfo")
public DeviceInfo insertDeviceInfo(User user, DeviceInfo deviceinfo)
throws OAuthRequestException {
EntityManager mgr = getEntityManager();
if (user != null)
deviceinfo.setUser(user);
else
throw new OAuthRequestException("Not authorized!");
try {
if (containsDeviceInfo(deviceinfo)) {
throw new EntityExistsException("Object already exists");
}
mgr.persist(deviceinfo);
} finally {
mgr.close();
}
return deviceinfo;
}
}
当我运行我的应用程序时,我总是从insertDeviceInfo
函数中获得以下异常:
09-26 15:55:18.185: E/com.safeandroid.mjmobilesolutions.GCMIntentService(3833): Exception received when attempting to register with server at https://velvety-347.appspot.com/_ah/api/
09-26 15:55:18.185: E/com.safeandroid.mjmobilesolutions.GCMIntentService(3833): com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
..
09-26 15:55:18.185: E/com.safeandroid.mjmobilesolutions.GCMIntentService(3833): "message" : "com.google.appengine.api.oauth.OAuthRequestException: Not authorized!",
我尝试将web_id
和android_id
放入凭据中,但它不起作用。怎么了?
答案 0 :(得分:0)
在Android代码中,作为受众群体,尝试在UI中找到“Web应用程序的客户端ID”。
credential = GoogleAccountCredential.usingAudience(this,
"server:client_id:putHereTheClientIDForWebApplication");
这对我有用, 其他事情似乎是正确的