5

我有一个使用Google Cloud Endpoints的应用程序。有些方法需要授权,所以我遵循this教程。这需要GET_ACCOUNTS权限。使用经Google授权的Google Cloud Endpoints和Google登录

我正在更新应用程序以使用运行时权限。我不喜欢请求读取联系人的权限,但GET_ACCOUNTS位于同一组中。正因为如此,我期待使用没有GET_ACCOUNTS权限的授权。

我认为Google Sign In可以工作,但我无法找到使用Google登录结果的方式。

这是一个用于创建对象打的电话到端点的代码:

Helloworld.Builder helloWorld = new Helloworld.Builder(AppConstants.HTTP_TRANSPORT, AppConstants.JSON_FACTORY,credential); 

凭据对象必须是HttpRequestInitializer但是从谷歌登录我得到一个GoogleSignInAccount

那么,有没有可能这样做?这应该怎么做?

回答

6

我终于找到了解决方案。使用教程发现here

必须在GoogleSignInOptions添加客户端ID:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
      .requestIdToken(CLIENT_ID) 
      .requestEmail() 
      .build(); 

在介绍之后,你会finaly得到GoogleSignInAccount。设定从GoogleSignInAccount令牌在GoogleCredential对象:

GoogleCredential credential = new GoogleCredential.Builder().setTransport(new NetHttpTransport()) 
      .setJsonFactory(JacksonFactory.getDefaultInstance()) 
      .build(); 
credential.setAccessToken(GoogleSignInAccount.getIdToken()); 

此证书是准备对谷歌云Enpoints验证的调用。

请注意,您必须从CLIENT_ID中删除“server:client_id:”部分。所以,如果你使用这样的:

credential = GoogleAccountCredential.usingAudience(this, 
    "server:client_id:1-web-app.apps.googleusercontent.com"); 

你CLIENT_ID是:

CLIENT_ID = "1-web-app.apps.googleusercontent.com" 

还要注意的是令牌有效的时间(在我的测试APROX 1小时),数量有限

+0

太棒了,我只是看着这个和你一样的原因(重做权限来支持运行时模型并想精简),非常感谢!我想知道这是否也解决了谷歌端点云端的问题,您需要重新加载授权用户,因为它没有ID ... – Creos

+0

不适用于我:后端接收空的com.google.appengine .api.users.User在授权失败时发生。你是否面临同样的问题? thx – Creos

+0

我编辑了答案。检查您是否正确设置了CLIENT_ID。你必须删除“server:client_id:”。我还发现令牌过期了,所以这只能在登录后的有限时间内运行。 – 9and3r

相关问题