2013-09-26 28 views
3

我正在开发一个使用GAE的Android应用程序,我试图进行认证的调用。我有一个问题,使一切工作。当我在授权函数(用户)中检查第一个参数时,它始终为空。 这里是我的Android代码:终端使用Android上的GAE进行授权。用户= null

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_idandroid_id置于凭证中,但它不起作用。怎么了?

+0

我有同样的事情挣扎似乎是正确的。它使用Javascript而不是Android。你有没有找到解决方案? – Floris

回答

0

在Android代码中,作为受众,请尝试在用户界面中找到“Web应用程序的客户端ID”。

credential = GoogleAccountCredential.usingAudience(this, 
     "server:client_id:putHereTheClientIDForWebApplication"); 

这为我工作, 其他的事情