[英]Add a token to Cloud Endpoints HTTP requests
我有一個使用Cloud Endpoints的API,並將其生成的客戶端庫添加到了我的Android應用程序中。
但是我不知道如何將我的身份驗證令牌添加到我的請求中。 目前,這是我知道如何發送的唯一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();
所以我的問題是:如何修改此代碼以添加我的身份驗證令牌:1)在POST請求的正文中2)或在授權標頭中
謝謝
您應該將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,它與您的項目緊密相關。 如果您沒有,則可以在控制台中創建它(您在項目-> API和身份驗證->憑據)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.