我正在创建一个应用程序,在那里我必须存储的文件在谷歌驱动器的用户登录,所以任何用户登录,他可以存储数据在他的驱动器上。现在我有一个问题,如何从代码生成谷歌驱动凭证(clientId,clientSecret和refreshToken),就像我看到的所有示例一样,请转到谷歌控制台并在那里执行that.Is,我们可以使用java代码来实现。实际上,我需要使用google drive广告数据库存储。使用下面的代码:但这需要客户端id。
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();
...
}
}发布于 2015-02-11 17:30:58
要获取客户端ID和客户端密钥,您需要首先在Google Developers控制台中创建或选择一个项目,并启用API。
您可以访问以下链接:
https://console.developers.google.com/apis/api/drive.googleapis.com/
发布于 2015-07-30 18:19:56
https://stackoverflow.com/questions/26987416
复制相似问题