2016-09-19 78 views
3

我想用授權碼上傳視頻到Youtube。如何在Youtube上使用java中的refreshtoken上傳視頻

 
public Credential authorize(List scopes, String credentialDatastore) throws IOException, URISyntaxException { 
     // Load client secrets. 
     URI filePath = new URI (GOOGLE_APIKEY); 
     Reader clientSecretReader =new InputStreamReader(new FileInputStream(filePath.toString())); 

     //Reader clientSecretReader = new InputStreamReader(Auth.class.getResourceAsStream(GOOGLE_APIKEY)); 
     GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, clientSecretReader); 



     // Checks that the defaults have been replaced (Default = "Enter X here"). 
     if (clientSecrets.getDetails().getClientId().startsWith("Enter") 
       || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) { 
      System.out.println(
        "Enter Client ID and Secret from https://console.developers.google.com/project/_/apiui/credential " 
          + "into src/main/resources/client_secrets.json"); 
      System.exit(1); 
     } 

     // This creates the credentials datastore at ~/.oauth-credentials/${credentialDatastore} 
     FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(System.getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY)); 
     DataStore datastore = fileDataStoreFactory.getDataStore(credentialDatastore); 

     GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, scopes).setCredentialDataStore(datastore).build(); 

     // Build the local server and bind it to port 8080 
     LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(8080).build(); 

     // Authorize. 
     return new AuthorizationCodeInstalledApp(flow, localReceiver).authorize("user"); 
    } 

這是工作,用戶必須驗證每次當視頻將上傳。 現在我想要使用我已有的refreshtoken生成的accesstoken上傳視頻。 但需要集成在我的Auth文件中,該文件具有LocalServerReceiver作爲內部使用Jetty服務器。

我寫了代碼來從刷新令牌獲取accesstoken。請幫助我整合它。


public GoogleCredential getCredentials(String clientId,String clientSecret,JsonFactory jsonFactory,HttpTransport transport,String refreshToken) throws IOException{ 

     GoogleCredential credential = new GoogleCredential.Builder() 
       .setClientSecrets(clientId, clientSecret) 
       .setTransport(transport) 
       .setJsonFactory(jsonFactory) 
       .build(); 
     credential.setRefreshToken(refreshToken); 

     // Do a refresh so we can fail early rather than return an unusable credential 

      credential.refreshToken(); 
     String authCode=credential.getAccessToken(); 
     return credential; 

    } 

+1

選中此[SO問題](http://stackoverflow.com/questions/31737699/upload-videos-to-youtube-from-my-web-server-in-java)如果它可以幫助你。 – KENdi

+0

我已經通過寫延伸的谷歌的AuthorizationCodeInstalledApp類的類克服這個問題,通過在三種不同的方法打破 1.getAuthorizationFromStorage 2.getAuthorizationFromGoogle 3.saveAuthorizationFromGoogle [實施代碼](HTTPS定製整個認證處理流程:/ /github.com/soumik-dutta/youtube-upload-javaAPI)和[擴展類](https://github.com/soumik-dutta/youtube-upload-javaAPI/blob/master/youtube/utils/ExtendedAuthorizationCodeInstalledApp。 java) –

+0

這是更好,如果你張貼爲答案:) – KENdi

回答

1

有明確,我是的Youtube視頻上傳過程中使用谷歌-Java的API

  1. 碼頭服務器實例,它會不斷聽取直到響應即將到來的情況下面臨兩個問題來自Google,如重定向網址中所述。
  2. 雖然有一個叫setHost()new LocalServerReceiver.Builder()類函數,它負責創建一個本地碼頭服務器實例,是throughing一個無法分配請求地址錯誤,每次在給定主機名不管是哪個並不重要港口。

整個授權過程在AuthorizationCodeInstalledApp類的授權方法,其主要功能如下

  1. 創建一個URL,它會要求用戶提供訪問應用程序來完成。
  2. 驗證成功後,會收到一個代碼(碼型服務器的一個實例不斷收聽,直到收到代碼)。
  3. 將剛剛收到的代碼與accesstoken和refreshtoken交換以供離線上傳。
  4. 存儲我們剛從Google收到的憑據。

解耦的整個過程我已經創建了一類新ExtendedAuthorizationCodeInstalledApp延伸原始AuthorizationCodeInstalledApp和創建的每個方法在class.The方法的每一個的功能如下

  1. getAuthorizationFromStorage:從存儲的憑證中獲取訪問令牌。
  2. getAuthorizationFromGoogle:使用來自Google的憑據進行身份驗證,創建將引導用戶進入身份驗證頁面並在狀態參數中創建自定義名稱 - 值對的URL。該值應該用base64編碼器進行編碼,這樣我們就可以在認證後收到從google重定向的相同代碼。
  3. saveAuthorizationFromGoogle:保存我們從Google獲得的憑據。
    • 創建從 credentialDatastorfrom從谷歌接收到響應後 認證GoogleAuthorizationCodeFlow對象。
    • 打穀歌獲得永久刷新令牌,可用於隨時獲取用戶的accesstoken 。
    • 商店一樣的accessToken令牌,並在文件名refreshtoken作爲 用戶ID

的代碼實現here

感謝@KENdi的建議...

相關問題