我正在开发一个使用 GAE 的 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
andandroid_id
放入凭据中,但没有用。怎么了?