Reputation: 1952
I would like to integrate Google Drive to our web application. So every user can connect his Google Drive storage to our application for storing files. What is the right authorization way for this purpose?
Is it OAuth right way? I know to create credentials in Developer console, but how to link google credentials with our account, store it and use later during uploading files?
I want to achieve writing/reading files to/from user's Drive after OAuth Login.
Here is my code, it's using service credentials. I know that it's wrong way.
public class GDManager {
private static String accountId = "[email protected]";
private static String p12File = "file.p12";
private static GoogleCredential buildCredential(HttpTransport httpTransport, JsonFactory jsonFactory) throws GeneralSecurityException, IOException {
// Build service account credential.
return new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(accountId)
.setServiceAccountScopes(Collections.singleton(DriveScopes.DRIVE))
.setServiceAccountPrivateKeyFromP12File(new java.io.File(GDManager.class.getClassLoader().getResource(p12File).getFile()))
.build();
}
private static Drive init() {
try {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredential credential = buildCredential(httpTransport, jsonFactory);
return new Drive.Builder(
httpTransport, jsonFactory, credential)
.setApplicationName("Google drive test")
.build();
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) throws IOException {
Drive service = init();
String parentId = null;
String mimeType = "text/plain";
File body = new File();
body.setTitle("test");
body.setDescription("description");
body.setMimeType(mimeType);
if (parentId != null && parentId.length() > 0) {
body.setParents(
Arrays.asList(new ParentReference().setId(parentId)));
}
java.io.File fileContent = new java.io.File("/home/michal/Downloads/google_drive");
FileContent mediaContent = new FileContent(mimeType, fileContent);
try {
File file = service.files().insert(body, mediaContent).execute();
System.out.println(file);
} catch (IOException e) {
System.out.println("An error occured: " + e);
}
}
}
Upvotes: 0
Views: 108
Reputation: 157
You can't test this from a static main method, you need to integrate this into your web application. The OAuth authentication require that you redirect the user's browser session to a Google Drive url for authorization. Check out this page for how to do this: https://developers.google.com/drive/web/credentials?hl=en. Note that when you call getCredentials without being authorized an exception is thrown and this contains the authorization url that you have to redirect the user's browser to in order to get the authorization code which you then exchange for an access token, which you can then use to access files on Google Drive.
Upvotes: 1