android - 在 Android 上使用 GAE 进行端点授权。用户=空

标签 android google-app-engine endpoints

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

最佳答案

在 Android 代码中,作为观众,请尝试将您可以在 UI 中找到的“Web 应用程序的客户端 ID”放入。

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

这对我有用, 其他事情似乎是正确的

关于android - 在 Android 上使用 GAE 进行端点授权。用户=空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19030555/

相关文章:

android - 使用 fragment 设计应用程序

google-app-engine - 如何将自定义信息从 App Engine 身份验证器传递到端点?

Reddit Endpoint 用于评论特定文章

Golang : implements http server health checking. gocraft/health

python - 了解 ndb 键类与 KeyProperty

android - 调用 GoogleAccountCredential.newChooseAccountIntent() 后未调用 onActivityResult

java - Android 无法对服务器进行 GET 改造 API 调用

android - Flutter:如何获取 Firebase 身份验证凭据以更新电子邮件和密码

android - 如何在android中的listview中删除复选框的焦点

ruby-on-rails - 将 Ruby on Rails 应用程序部署到 Google App Engine