How we can create the folder in google drive by using java

痞子三分冷 提交于 2019-12-13 08:48:22

问题


I need to create the folder in Google drive by using Java. Does any one tell me the example or how to create the folder in Google drive. Thanks in advance...!!!

My program

package net.sf.dynamicreports.examples;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import com.google.gson.JsonObject;

public class SourceCodeProgram {

    public static void main(String argv[]) throws Exception {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost post = new HttpPost(
                "https://www.googleapis.com/drive/v2/files");
        post.addHeader("Content-Type", "application/json");
        post.addHeader("Authorization",
                "Bearer  XXXXXXXXXXXXXXXXXXXXXXXXX ");

        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("title", "Test folder");
        jsonObject
                .addProperty("mimeType", "application/vnd.google-apps.folder");

        post.setEntity(new StringEntity(jsonObject.toString()));
        httpClient.execute(post);
    }
}

In above program everything is going fine just want to know about the

post.addHeader("Authorization", "Bearer XXXXXXXXXXXXXXXXXXXXXXXXX ");

What should i place at

XXXXXXXXXXXXXXXXXXXXXXXXX

is it some kind of key which will i get from google?


回答1:


Google uses OAuth 2.0 authentication, you need to add OAuth Token in place of xxxxxxxx for OAuth 2.0 authentication (Authorization Bearer header is used for OAuth authentication purpose). This may be useful for you : https://developers.google.com/identity/protocols/OAuth2

After successful authentication with Google, you can use following code for creating the folder in Google Drive:

File fileMetadata = new File();
fileMetadata.setName("Invoices");
fileMetadata.setMimeType("application/vnd.google-apps.folder");

File file = driveService.files().create(fileMetadata).setFields("id").execute();
System.out.println("Folder ID: " + file.getId());

P.S. - This is referenced from Google Drive Help Link




回答2:


Use this Drive API JAVA Quickstart instead and start from there. The 'xxxxxxxx' is not something you can copy and paste from somewhere. It is an access token generated by Google API. But for testing purposes, you can generate it from OAuthplayground, copy and paste in place of xxxxx. It will only last an hour so you also need to implement refresh tokens. Again that's for testing purposes only.

To give you an idea of the access token being generated, you can see a Javascript implementation of this in Picker API:

function onAuthApiLoad() {
        window.gapi.auth.authorize(
            {
              'client_id': clientId,
              'scope': scope,
              'immediate': false
            },
            handleAuthResult);
      }

 function handleAuthResult(authResult) {
        if (authResult && !authResult.error) {
          oauthToken = authResult.access_token; //<-- access token 
          createPicker();
        }
      }


来源:https://stackoverflow.com/questions/46048395/how-we-can-create-the-folder-in-google-drive-by-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!