0

我的項目是一個java託管在heroku上的火花項目。該項目的一部分要求我將我的谷歌驅動器中的特定文件下載到應用程序中。當我的應用程序在本地運行時,我設法設置了所有內容,但只要我部署了應用程序,它就停止工作。這是不管這個事實,當應用程序在本地運行時,我使用了一個Oauth2類型的客戶端ID,當我部署應用程序時,我創建了一個新類型的Web應用程序。谷歌驅動器Oauth 2.0的Java Web應用程序

下面的認證和下載的代碼片段:

public class AutoCalls { 

    /** Application name. */ 
    private static final String APPLICATION_NAME = "calls-made"; 
    private static final java.io.File DATA_STORE_DIR = new java.io.File(System.getProperty("target/classes/auth"), ".credentials/calls-made"); 
    private static FileDataStoreFactory DATA_STORE_FACTORY; 
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); 
    private static HttpTransport HTTP_TRANSPORT; 
    private static final List<String> SCOPES = Arrays.asList(DriveScopes.DRIVE); 

    static { 
     try { 
      HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); 
      DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR); 

     } catch (GeneralSecurityException | IOException t) { 
      System.exit(1); 
     } 
    } 

    public static void startDownload() throws IOException, ParseException { 

     Drive serv = getDriveService(); 
    // drive stuff deleted 
    } 

    public static Credential authorize() throws IOException { 
     InputStream in = new FileInputStream("target/classes/auth/client_secret_*****************************************.apps.googleusercontent.com.json"); 
     GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); 
     GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) 
      .setDataStoreFactory(DATA_STORE_FACTORY) 
      .setAccessType("offline") 
      .build(); 
     Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); 
     System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath()); 
     return credential; 
    } 

    public static Drive getDriveService() throws IOException { 
     Credential credential = authorize(); 

     return new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) 
      .setApplicationName(APPLICATION_NAME) 
      .build(); 
    } 
} 

目前,當我嘗試啓動下載我得到了Heroku的一個URL記錄一條如下:

2017-02-20T10:32:28.656820+00:00 app[web.1]: Please open the following address in your browser: 
2017-02-20T10:32:28.656908+00:00 app[web.1]: https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=************-vvdi7u6bmp1vc90sdidtnuiftdi1t49c.apps.googleusercontent.com&redirect_uri=http://localhost:48085/Callback&response_type=code&scope=https://www.googleapis.com/auth/drive 

當我嘗試在瀏覽器中打開URL我得到一個錯誤:

Error: redirect_uri_mismatch 

我找不到任何好的一步一步的導師ial訪問一個Java Web應用程序的谷歌驅動器內容,所以任何幫助將arreiciated。

回答

0

通常,如果OAuth在服務器A上工作,但不在服務器B上工作,這是因爲重定向URL尚未在API控制檯上正確配置。沒有理由爲什麼您需要使用不同的客戶端ID,因爲可以使用多個URL配置單個客戶端ID,例如您的本地主機和您的heroku服務器。另一種可能性是使用https。

在代碼中有一個單獨的問題,您在lista.isEmpty()上迭代。你應該迭代nextPageToken != null。請參閱Google drive rest API, Download files from root folder only的答案

+0

是否我在本地運行應用程序然後將其移到Heroku的事實沒有意義?我問,因爲客戶端ID可以用於Web應用程序和其他(對於桌面應用程序,例如沒有重定向URL)等。工作的客戶端ID是其他類型,因此不接受重定向URL。 – Chognificent

+0

我不明白你爲什麼改變了客戶端類型。當然,您的應用程序在測試中應該和生產中一樣。因此,標準方法僅僅是爲您的測試環境和生產環境提供一個包含重定向URL的客戶端ID。無論是我誤解了你正在努力實現的目標,還是你誤解了OAuth /客戶端類型。 – pinoyyid

+0

我改變了我的客戶端類型,因爲我使用的(其他)不允許重定向URL,因此無法正常工作。 – Chognificent

相關問題