无需使用OAuth将文件上传到java中的谷歌驱动器 [英] Upload file to google drive in java without OAuth

查看:285
本文介绍了无需使用OAuth将文件上传到java中的谷歌驱动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Google云端硬盘中上传文件到我的帐户。这个过程应该没有人为干预。有没有办法上传一个文件在我的谷歌驱动器使用谷歌驱动器API提供我的用户和我的密码?

I want upload a file to my account in google drive. This process should be without human intervention. Is there any way to upload a file in my google drive using the google drive api providing my user and my password?

我到目前为止所做的一切:

What I've done so far:

  • I created a project in https://console.developers.google.com and enable the google drive api
  • In https://console.developers.google.com -> APIs & Auth -> Credentials I created a new client id, in application type I selected Service Account and I downloaded the pkcs12 key.
  • I have the next code:

/**
 * Email of the Service Account
 */
private static final String SERVICE_ACCOUNT_EMAIL = "account_of_service_account@developer.gserviceaccount.com";

/**
 * Path to the Service Account's Private Key file
 */
private static final String SERVICE_ACCOUNT_PKCS12_FILE_PATH = "/path/to/pkcs12/donloaded.pkcs12";

/**
 * Build and returns a Drive service object authorized with the service
 * accounts.
 *
 * @return Drive service object that is ready to make requests.
 */
public static Drive getDriveService() throws GeneralSecurityException, IOException, URISyntaxException {
    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();
    List<String> scopes = new ArrayList<String>();
    scopes.add(DriveScopes.DRIVE);
    GoogleCredential credential;
    credential = new GoogleCredential.Builder()
            .setTransport(httpTransport)
            .setJsonFactory(jsonFactory)
            .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
            .setServiceAccountScopes(scopes)
            .setServiceAccountPrivateKeyFromP12File(new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
            .build();
    Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
            .setHttpRequestInitializer(credential).build();
    return service;
}

public static void main(String[] args) throws GeneralSecurityException, IOException, URISyntaxException {

    Drive client = getDriveService();

    File body = new File();
    body.setTitle("mytitle");
    body.setMimeType("application/vnd.google-apps.spreadsheet");

    File file = client.files().insert(body).execute();

    System.out.println(file.getId());
    System.out.println(file.getAlternateLink());
}


代码的结果是好的,我得到文件的ID和备用链接,但文件没有加载到我的帐户。我在浏览器中粘贴备用链接,但没有权限。

When I run the code the result is ok, I get file's id and the alternate link, but the file not load to my account. I paste the alternate link in a browser but I not have permission.

您刚刚上传的文件在哪里?我应该更改什么以表明直至我的帐户驱动器?我应该在哪里添加我的用户名和密码才能与我的驱动器相关联?我应该怎么做文件才能公开?

Where is the file you just uploaded? What should I change to indicate that up to my account drive? Where should I add my username and password to be associated with my drive? What should I do to the file to become public?

非常感谢您的关注。我希望他们能帮助我。

Thank you very much for all your attention. I wish they can help me.

推荐答案

您正在将文件上传到您的服务帐户。
如果您需要访问该文件,它必须与您的电子邮件共享。
使用以下方法与您的电子邮件共享文件。

You are uploading the file into your service account. If you need to access the file, it must be shared with your email. use the following method to share the file with your email.

 public static void shareFileOrFolder(Drive service, File file)
        throws IOException {
    Permission newPermission = new Permission();

    newPermission.setEmailAddress("xxxxx@gmail.com");
    newPermission.setValue("xxxxx@gmail.com");
    newPermission.setType("user");
    newPermission.setRole("writer");
    service.permissions().insert(file.getId(), newPermission).execute();
}

这篇关于无需使用OAuth将文件上传到java中的谷歌驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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