android - 向 Cloud Endpoints HTTP 请求添加 token

标签 android google-app-engine google-cloud-endpoints

我有一个使用 Cloud Endpoints 的 API,我将其生成的客户端库添加到我的 Android 应用中。

但是我不知道如何将我的授权 token 添加到我的请求中。现在,这是我知道如何发送的唯一 HTTP 请求:

DrinkEndpoint.Builder builder = new DrinkEndpoint.Builder(AndroidHttp.newCompatibleTransport(),new GsonFactory(), null);
            DrinkEndpoint service = builder.build();
            Drink drink = new Drink();
            drink.setName(params[0]);
            response = service.insertDrink(drink).execute();

所以我的问题是:如何修改此代码以添加我的身份验证 token : 1)在POST请求体中 2) 或在授权 header 中

谢谢

最佳答案

您应该将 GoogleAccountCredential 作为最后一个参数传递

new DrinkEndpoint.Builder(AndroidHttp.newCompatibleTransport(),
            AndroidJsonFactory.getDefaultInstance(), googleAccountCredential);

如何使用 G+ 范围构建凭证的示例:

GoogleApiClient googleApiClient = new GoogleApiClient.Builder(app)
        .addApi(Plus.API)
        .addScope(Plus.SCOPE_PLUS_PROFILE)
        .build();

GoogleAccountCredential googleAccountCredential =
     GoogleAccountCredential.usingAudience(context, "server:client_id:" + WEB_CLIENT_ID);
     googleAccountCredential.setSelectedAccountName(userEmail);

WEB_CLIENT_ID 来自 Google Developers Console,它与您的项目紧密相关。如果你没有,你可以在控制台中创建它(你的项目 -> APIs & auth -> Credentials)。

关于android - 向 Cloud Endpoints HTTP 请求添加 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27564336/

相关文章:

java - 在单元测试中找不到任何支持 AES/CBC/ISO7816-4Padding 的提供商

java - 无法再编译

android - 无法使用 NotificationListenerService 取消通知

python - 自 1.8.2 更新后出现错误

google-app-engine - 如何在Google云中部署现有的Spring MVC + JPA + MYSql + Maven应用程序

javascript - 我怎样才能使 go Time 与 js Date 兼容

android - 调用 geoCoder.getFromLocation() 时服务不可用

google-app-engine - 限制我的 Google App Engine 应用程序访问 Google API key

python - 如何使用python解析GAE中url中的散列参数(用#而不是?)?

ios - iOS 中使用 Google 登录的 Google Endpoints