2014-12-19 27 views
1

我有一个使用云端点的API,并将其生成的客户端库添加到我的Android应用中。将令牌添加到云端点HTTP请求

但是我不知道如何将我的身份验证令牌添加到我的请求中。现在,这里是唯一的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(); 

所以我的问题是:如何修改这个代码来添加我的身份验证令牌:在POST请求的主体 1) 2 ),或在授权头

谢谢

回答

1

你应该通过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来自谷歌开发者控制台,并紧到您的项目。如果你没有,你可以在控制台中创建它(你的项目 - > API & auth - > Credentials)。

+0

好的,这将是有益的,谢谢。你还可以告诉我如何添加另一个对象到我的HTTP请求? (例如非谷歌令牌或任何其他类型的字符串)现在我只知道如何添加我的实体的实例或谷歌令牌。 – Kritias 2014-12-19 23:28:13

+0

@Kritias不幸的是我不知道该怎么做。这应该是另一个问题,也许有人会在这个问题上帮助你。 – tomrozb 2014-12-20 09:50:36

+0

好的,谢谢@tomrozb – Kritias 2014-12-20 13:19:59

相关问题