本文介绍了授权给Google客户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在写一个创建授权给Bigquery和Google云端存储的课程。在过去,我使用了已弃用的CredentialStore。我试图使用DataStoreFactory,但是我发现它允许我只使用StoredCredential,而我需要一个Credential。我知道可以从Credential转换为StoredCredential,但我不知道如何将它们转换为相反方向(StoredCredential转换为Credential)。我创建我的连接使用例如
Storage.Builder(HttpTransport传输,JsonFactory jsonFactory,HttpRequestInitializer httpRequestInitializer)

任何人都可以指示我如何做到这一点?谢谢!

解决方案

在大多数情况下,无论您使用Credential,都可以使用StoredCredential。只有一点你可以使用Credential,它在OAuth回调期间检索访问令牌。从那里,Credential可以转换为StoreCredential并存储到DataStore中。之后,存储和检索所有与StoredCredential一起工作。



但有些地方是StoredCredential不能使用。我刚刚遇到想要创建Google Drive API Service包装的人。



有一种方法可以通过GoogleCredential对象来解决这个问题,它可以根据每个StoredCredential创建这个答案:

  import com.google.api.client.auth.oauth2.StoredCredential; 

public static GoogleCredential createGoogleCredential(StoredCredential storedCredential)
{
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleCredential googleCredential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setClientSecrets(client_id,client_secret)。build() ;
googleCredential.setAccessToken(storedCredential.getAccessToken());
返回googleCredential;
}


I am writing a class for creating authorization to Bigquery and Google Cloud Storage. In the past I have used CredentialStore which has been deprecated. I am trying to use DataStoreFactory but I discovered that it allows me to use only StoredCredential while I need a Credential. I know one can convert from Credential to StoredCredential but I am not sure how to convert them in the opposite direction (StoredCredential to Credential). I am creating my connection using for exampleStorage.Builder(HttpTransport transport, JsonFactory jsonFactory, HttpRequestInitializer httpRequestInitializer)

Could anyone point me in a direction about how to achieve this? Thank you!

解决方案

In most cases, wherever you use Credential, you could use StoredCredential. There is only one point you would work with Credential, which is retrieving the Access Token during the OAuth callback. From there the Credential can be converted to StoreCredential and stored into the DataStore. After that storage and retrieval all works with StoredCredential.

But there are places were StoredCredential can't be used. I just encountered one trying to create the Google Drive API Service wrapper.

There is a way to get around this with the GoogleCredential object, it can be created from StoredCredential as per this answer: Stored Credential from google api to be reused java

import com.google.api.client.auth.oauth2.StoredCredential;

public static GoogleCredential createGoogleCredential(StoredCredential storedCredential)
{
    HttpTransport httpTransport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    GoogleCredential googleCredential = new GoogleCredential.Builder()
        .setTransport(httpTransport)
        .setJsonFactory(jsonFactory)
        .setClientSecrets("client_id", "client_secret").build();
    googleCredential.setAccessToken(storedCredential.getAccessToken());
    return googleCredential;
}

这篇关于授权给Google客户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 02:57