通过Google登录使用授权的Google Cloud Endpoints [英] Use authorized Google Cloud Endpoints with Google Sign in

查看:271
本文介绍了通过Google登录使用授权的Google Cloud Endpoints的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一款使用Google Cloud Endpoints的应用。某些方法需要授权,因此我遵循教程。这需要GET_ACCOUNTS权限。



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



我认为 Google登录可以工作,但我无法找到一种方法来使用Google登录的结果。



这是用于创建对象以调用端点的代码:

  Helloworld.Builder helloWorld =新的Helloworld.Builder(AppConstants.HTTP_TRANSPORT,AppConstants.JSON_FACTORY,凭证); 

凭证对象必须是 HttpRequestInitializer 但来自Google登录我收到 GoogleSignInAccount

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

解决方案

我终于找到了解决方案。使用该教程在此处找到了



您必须在GoogleSignInOptions中添加客户ID:

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

遵循本教程,您将获得GoogleSignInAccount。在GoogleCredential对象中设置来自GoogleSignInAccount的令牌:

  GoogleCredential凭证=新的GoogleCredential.Builder()。setTransport(new NetHttpTransport ))
.setJsonFactory(JacksonFactory.getDefaultInstance())
.build();
credential.setAccessToken(GoogleSignInAccount.getIdToken());

此凭证已准备好对Google云端点进行认证呼叫。



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

pre $ lt; code> 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小时)


I have an app that uses Google Cloud Endpoints. Some methods need authorization so I followed this tutorial. This requires the GET_ACCOUNTS permissions.

I am updating the app to work with runtime permissions. I do not like to request permission to read contacts, but GET_ACCOUNTS is in the same group. Because of this I am looking to use authorization without GET_ACCOUNTS permission.

I think that Google Sign In could work but I am unable to find a way to use the result from Google Sign In.

This is the code used to create the object to make the calls to the endpoint:

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

The credential object must be a HttpRequestInitializer but from the Google Sign In I get a GoogleSignInAccount.

So, is it possible to do this? How should this be done?

解决方案

I finally found the solution. Using the tutorial found here.

You must add the Client id in the GoogleSignInOptions:

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

Following the tutorial you will finaly get a GoogleSignInAccount. Set the token from the GoogleSignInAccount in a GoogleCredential object:

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

This credential is ready to make authenticated calls to Google Cloud Enpoints.

Note that you must remove "server:client_id:" part from the CLIENT_ID. So if you were using this:

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

Your CLIENT_ID would be:

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

Also note that the token is valid for a limited amount of time (Aprox. 1 hour in my testing)

这篇关于通过Google登录使用授权的Google Cloud Endpoints的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆