Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
615 views
in Technique[技术] by (71.8m points)

oauth 2.0 - Google drive api services account view uploaded files to google drive using 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);
          }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. Go to you Google Drive. Create some folder, and open "Sharing settings" for it.
  2. Add your service email to the list of allowed users and allow it to edit the contents of that folder.
  3. Remember folder ID somewhere and use this folder id while uploading files.
  4. It will be uploaded in your personal google drive account.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...