在大多数情况下,无论您在哪里使用
Credential,都可以使用
StoredCredential。您只需要使用一点
Credential,那就是在
OAuth
回调过程中检索访问令牌。从那里
Credential可以将转换为
StoreCredential并存储到中
DataStore。之后,所有存储和检索均与配合使用
StoredCredential。
但是有些地方是
StoredCredential无法使用的。我刚遇到一个尝试创建Google Drive API Service包装器的应用程序。
有一种解决该
GoogleCredential对象的方法,可以
StoredCredential根据以下答案创建该对象:
来自GoogleAPI的存储凭据可以使用Java进行重用
import com.google.api.client.auth.oauth2.StoredCredential;public static GoogleCredential createGoogleCredential(StoredCredential storedCredential) { GoogleCredential googleCredential = new GoogleCredential.Builder() .setTransport(new NetHttpTransport()) .setJsonFactory(new JacksonFactory()) .setClientSecrets("client_id", "client_secret") .setAccessToken(storedCredential.getAccessToken()) .build(); return googleCredential;}


