获取警告使用Java将文件上传到Google驱动器时

时间:2014-05-24 06:42:06

标签: java api google-api google-drive-api

我尝试使用Java将文件上传到我的Google云端硬盘帐户。文件已上传,但我收到警告:警告:未设置应用程序名称。调用Builder#setApplicationName

代码是

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

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.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;


public class UploadFileGoogleDrive {
      private static String CLIENT_ID = "*****";
      private static String CLIENT_SECRET = "******";
      private static String REDIRECT_URI = "****";

    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("online")
            .setApprovalPrompt("auto").build();

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

        GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();
        GoogleCredential credential = new GoogleCredential().setFromTokenResponse(response);

        //Create a new authorized API client
        Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).build();

        //Insert a file  
        File body = new File();
        body.setTitle("My document");
        body.setDescription("A test document");
        body.setMimeType("text/plain");

        java.io.File fileContent = new java.io.File("document.txt");
        FileContent mediaContent = new FileContent("text/plain", fileContent);

        File file = service.files().insert(body, mediaContent).execute();
        System.out.println("File ID: " + file.getId());
    }

}

输出 2014年5月24日上午11:59:14 com.google.api.client.googleapis.services.AbstractGoogleClient 警告:未设置应用程序名称。调用Builder#setApplicationName。 文件编号:1Ze77mqHtKWDU3eVljATlHQ0U

1 个答案:

答案 0 :(得分:0)

适用于Java的Google API客户端库强烈建议您在构建客户端时设置应用程序名称,以便在对我们的服务器日志的请求进行故障排除时使用。您可以在Google Drive Java command line sample中看到有关如何设置应用程序名称的示例。