我有一个使用 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/