将授权的Google Cloud Endpoints与Google登录一起使用

时间:2022-12-17 23:11:09

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

我有一个使用Google Cloud Endpoints的应用。有些方法需要授权才能遵循本教程。这需要GET_ACCOUNTS权限。

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.

我正在更新应用程序以使用运行时权限。我不想请求阅读联系人的权限,但GET_ACCOUNTS属于同一组。因此,我希望在没有GET_ACCOUNTS许可的情况下使用授权。

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

我认为Google登录可行,但我无法找到使用Google登录结果的方法。

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.

凭证对象必须是HttpRequestInitializer,但是从Google登录我获得GoogleSignInAccount。

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

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

1 个解决方案

#1


6  

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

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

You must add the Client id in the GoogleSignInOptions:

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

 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:

在本教程之后,您将最终获得GoogleSignInAccount。在GoogleCredential对象中设置来自GoogleSignInAccount的令牌:

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.

此凭据已准备好对Google Cloud Enpoints进行经过身份验证的调用。

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

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

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

Your CLIENT_ID would be:

您的CLIENT_ID将是:

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)

另请注意,令牌在有限的时间内有效(Aprox。在我的测试中1小时)

#1


6  

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

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

You must add the Client id in the GoogleSignInOptions:

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

 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:

在本教程之后,您将最终获得GoogleSignInAccount。在GoogleCredential对象中设置来自GoogleSignInAccount的令牌:

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.

此凭据已准备好对Google Cloud Enpoints进行经过身份验证的调用。

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

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

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

Your CLIENT_ID would be:

您的CLIENT_ID将是:

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)

另请注意,令牌在有限的时间内有效(Aprox。在我的测试中1小时)