-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriveRefreshGen.java
More file actions
87 lines (77 loc) · 4.29 KB
/
DriveRefreshGen.java
File metadata and controls
87 lines (77 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* Alec Snyder
* cs162
* Google Drive for linux
* grabs a refresh token and saves it to .drive metafile
* code MODIFIED FROM Google drive Quickstart code: https://developers.google.com/drive/web/quickstart/quickstart-java
*/
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.auth.oauth2.TokenResponseException;
import com.google.api.client.http.FileContent;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class DriveRefreshGen {
private static String CLIENT_ID = "842617857460-1gm3qknepc16b9dei9brhgnkc12aqrds.apps.googleusercontent.com";
private static String CLIENT_SECRET = "d7lWsLVBBOBQw4AkQxSE5sYH";
private static String REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";
public static String getURL() //separate helper method accessible outside the class to get the url
{
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET, Arrays.asList(DriveScopes.DRIVE))
.setAccessType("offline")
.setApprovalPrompt("force").build();
//give authorization URL
String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build();
return url;
}
public static void genRefresh(String authcode) throws IOException
{
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET, Arrays.asList(DriveScopes.DRIVE))
.setAccessType("offline")
.setApprovalPrompt("force").build();
GoogleTokenResponse response = flow.newTokenRequest(authcode).setRedirectUri(REDIRECT_URI).execute();
GoogleCredential credential = new GoogleCredential.Builder().setJsonFactory(new JacksonFactory()).setTransport(new NetHttpTransport()).setClientSecrets(CLIENT_ID, CLIENT_SECRET).build();
credential.setFromTokenResponse(response);
//save refresh token for later
EasyWriter writer=new EasyWriter(System.getProperty("user.home")+"/gdrive/.drive_key");
writer.println(credential.getRefreshToken());
writer.close();
}
public static void main(String[] args) throws IOException {
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET, Arrays.asList(DriveScopes.DRIVE))
.setAccessType("offline")
.setApprovalPrompt("force").build();
//give authorization URL
String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).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();
//grab code, build credential and parse refresh token
GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();
GoogleCredential credential = new GoogleCredential.Builder().setJsonFactory(new JacksonFactory()).setTransport(new NetHttpTransport()).setClientSecrets(CLIENT_ID, CLIENT_SECRET).build();
credential.setFromTokenResponse(response);
//save refresh token for later
EasyWriter writer=new EasyWriter(System.getProperty("user.home")+"/gdrive/.drive_key");
writer.println(credential.getRefreshToken());
writer.close();
}
}