我们如何使用java在谷歌驱动器中创建文件夹 [英] How we can create the folder in google drive by using java

查看:362
本文介绍了我们如何使用java在谷歌驱动器中创建文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Java在Google驱动器中创建文件夹。是否有人告诉我该示例或如何在Google驱动器中创建文件夹。提前致谢...!!!



我的程序

  package net.sf.dynamicreports.examples; 
导入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);






$ b

在上面的程序中,一切都很好,只是想知道关于

post.addHeader(Authorization,
Bearer XXXXXXXXXXXXXXXXXXXXXXXXX);

我应该把什么放在


XXXXXXXXXXXXXXXXXXXXXXXXX

是什么样的关键,我会从谷歌获得?

解决方案

使用此 Drive API JAVA Quickstart ,然后从那里开始。 'xxxxxxxx'不是你可以从某处复制和粘贴的东西。它是由Google API生成的访问令牌。但出于测试目的,您可以从OAuthplayground生成它,复制并粘贴代替xxxxx。它只会持续一个小时,因此您还需要实施刷新令牌。再次,这只是为了测试的目的。

为了让您了解访问令牌的生成过程,您可以在 Picker API

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

函数handleAuthResult(authResult){
if(authResult&&!authResult.error){
oauthToken = authResult.access_token; //< - 访问令牌
createPicker();
}
}


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?

解决方案

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();
        }
      }

这篇关于我们如何使用java在谷歌驱动器中创建文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆