Java 如何在本地存储Google Drive SDK的凭据
我正在开发一个基于Java的桌面应用程序,需要从用户的GoogleDrive帐户下载一些文件。我已经研究过了,到目前为止,我已经得出了以下代码:Java 如何在本地存储Google Drive SDK的凭据,java,oauth-2.0,google-drive-api,Java,Oauth 2.0,Google Drive Api,我正在开发一个基于Java的桌面应用程序,需要从用户的GoogleDrive帐户下载一些文件。我已经研究过了,到目前为止,我已经得出了以下代码: public class Main { public static void main(String[] args) { String clientId = "..."; String clientSecret = "..."; HttpTransport httpTransport = new NetHttpTran
public class Main
{
public static void main(String[] args)
{
String clientId = "...";
String clientSecret = "...";
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport,
jsonFactory,
clientId,
clientSecret,
Arrays.asList(DriveScopes.DRIVE)
)
.setAccessType("online")
.setApprovalPrompt("auto").build();
String redirectUri = "urn:ietf:wg:oauth:2.0:oob";
String url =
flow
.newAuthorizationUrl()
.setRedirectUri(redirectUri)
.build();
System.out.println("Please open the following URL in your browser then type the authorization code:");
System.out.println(" " + url);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String code = br.readLine();
GoogleTokenResponse response =
flow
.newTokenRequest(code)
.setRedirectUri(redirectUri)
.execute();
GoogleCredential credential =
new GoogleCredential()
.setFromTokenResponse(response);
Drive service =
new Drive.Builder(httpTransport, jsonFactory, credential)
.build();
...
}
}
这是可行的,但每次都需要用户授权应用程序(即在浏览器中打开给定的URL并复制授权令牌)。我需要以这样一种方式实现应用程序,即当用户仅第一次运行应用程序时,需要用户对其进行授权。然后,应用程序将在本地存储某种秘密令牌,以便下次使用
我已经彻底地研究了文档,但没有找到关于如何实现这一目标的充分解释(尤其是在桌面应用程序中)
如何做到这一点?在阅读关于accessType=“离线”的文章时。这将返回一个刷新令牌,它是您正在寻找的可存储的“秘密令牌”。保存此内容,您可以将其转换为身份验证令牌,而无需任何用户干预。步骤1:使用脱机访问类型生成URL
flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET, Arrays.asList(DriveScopes.DRIVE))
.setAccessType("offline")
.setApprovalPrompt("auto").build();
String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build();
步骤2:存储凭证accessToken和refreshToken。代码=以上url的响应代码
GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();
GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setClientSecrets(CLIENT_ID, CLIENT_SECRET)
.build()
.setFromTokenResponse(response);
String accessToken = credential.getAccessToken();
String refreshToken = credential.getRefreshToken();
步骤3:在需要时重用令牌
GoogleCredential credential1 = new GoogleCredential.Builder().setJsonFactory(jsonFactory)
.setTransport(httpTransport).setClientSecrets(CLIENT_ID, CLIENT_SECRET).build();
credential1.setAccessToken(accessToken);
credential1.setRefreshToken(refreshToken);
Drive service = new Drive.Builder(httpTransport, jsonFactory, credential1).build();
步骤4:了解OAuth以处理错误和刷新令牌您能否更具体地说明如何更新代码?我似乎无法让它工作。提前谢谢!在流…构建中添加.setAccessType(“脱机”)。这里有一个例子。或者,您可以直接按此处所述调用url,为什么要让用户打开浏览器url并复制令牌?为什么不urlfetch呢?@ZigMandel我不明白你说的urlfetch令牌是什么意思。你能解释一下吗?请看google oauth文档中的客户端类型流。这里有一些例子。