谷歌驱动api服务账户查看上传文件到谷歌驱动使

时间:2023-03-19
本文介绍了谷歌驱动api服务账户查看上传文件到谷歌驱动使用java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了代码以使用 java 中的服务帐户将文件插入谷歌驱动器.文件已上传到谷歌驱动器,但我无法使用网络浏览器看到它.当我使用代码检查文件时,我可以检索上传的文件.我的问题是如何将文件上传到谷歌驱动器,可以通过网络浏览器在 java 中使用谷歌服务帐户查看

I have created code to insert file to google drive using services account in java .file is uploaded to the google drive but i cannot see it using web browser .when i check file using code i can retrieve the uploaded file .my question is how can i upload files to google drive that can be viewed by web browser using google services account in java

请在下面找到示例代码.

please find the sample code below.

      public class PlusServiceAccountSample {


          private static final String APPLICATION_NAME = "";

          /** E-mail address of the service account. */
          private static final String SERVICE_ACCOUNT_EMAIL = "XXXXXXXX@developer.gserviceaccount.com" ;

          /** Global instance of the HTTP transport. */
          private static HttpTransport httpTransport;

          /** Global instance of the JSON factory. */
          private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();


          public static void main(String[] args) {
            try {
              try {
                httpTransport = GoogleNetHttpTransport.newTrustedTransport();
                // check for valid setup
                if (SERVICE_ACCOUNT_EMAIL.startsWith("Enter ")) {
                  System.err.println(SERVICE_ACCOUNT_EMAIL);
                  System.exit(1);
                }
                String p12Content = Files.readFirstLine(new File("XXXXX-privatekey.p12"), Charset.defaultCharset());
                if (p12Content.startsWith("Please")) {
                  System.err.println(p12Content);
                  System.exit(1);
                }
                // service account credential (uncomment setServiceAccountUser for domain-wide delegation)
                GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
                    .setJsonFactory(JSON_FACTORY)
                    .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
                    .setServiceAccountScopes(Collections.singleton(DriveScopes.DRIVE))
                    .setServiceAccountPrivateKeyFromP12File(new File("XXXXXXXX-privatekey.p12"))

                    .build();


                Drive service = new Drive.Builder(httpTransport, JSON_FACTORY,credential).build();

                com.google.api.services.drive.model.File  file = new com.google.api.services.drive.model.File();
                file.setTitle("testdd");
                file.setMimeType("application/vnd.google-apps.spreadsheet");
                Drive.Files.Insert insert = null;
                try {
                    insert = service.files().insert(file);
                    file = insert.execute();


                } catch (Exception e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }

                com.google.api.services.drive.model.File uploadedDoc = service.files().get("1ZJnkgFXAiUNSBeq3DmhHGqXn7-v37vLg1UsRekRQNjU").execute();

                System.out.println("Title: " + uploadedDoc.getTitle());




                return;
              } catch (IOException e) {
                System.err.println(e.getMessage());
              }
            } catch (Throwable t) {
              t.printStackTrace();
            }
            System.exit(1);
          }

推荐答案

如果您为文件设置了权限,则可以在您的常规 gmail 帐户(与我共享"部分)中看到它:

If you set permissions for the file, you can then see it in your regular gmail acount ("Shared with me" section):

Permission newPermission = new Permission();
newPermission.setValue("...your regular username...@gmail.com");
newPermission.setType("user");
newPermission.setRole("reader");
service.permissions().insert(file.getId(), newPermission).execute();

您还可以使用唯一的 url 来获取文件:

You can also use the unique url to get to the file:

System.out.println("Title: " + uploadedDoc.getTitle()+ " "+ uploadedDoc.getAlternateLink());

类型和角色的可能值在 Google Drive SDK 文档中:https://developers.google.com/drive/v2/reference/permissions/insert

The possible values for Type and Role are in Google Drive SDK docs: https://developers.google.com/drive/v2/reference/permissions/insert

这篇关于谷歌驱动api服务账户查看上传文件到谷歌驱动使用java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:Google Drive 服务帐号返回 403 usageLimits 下一篇:Google Drive Android API - 无效的 DriveId 和 Null Resourc

相关文章

最新文章