2016-09-19 3 views
3

私はYoutubeに認証コードで動画をアップロードしようとしています。Javaでrefreshtokenを使用してYouTubeに動画をアップロードする方法

 
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を使ってビデオをアップロードします。 しかし、LocalServerReceiverが内部でJettyサーバーを使用しているAuthファイルに統合する必要があります。

私はリフレッシュトークンから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

は、私はGoogleのAuthorizationCodeInstalledAppクラスを拡張するクラスを記述することによってこの問題を克服し、 3.saveAuthorizationFromGoogle [実装コード](httpsの三つの異なる方法で 1.getAuthorizationFromStorage 2.getAuthorizationFromGoogleを壊すことによって、全体の認証プロセス・フローをカスタマイズしています/ /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. 応答が来てまで、絶えずリスニングされます桟橋サーバーインスタンスのインスタンスがありましたリダイレクトURLに記載されているようにGoogleから送信されます。
  2. は毎回アドレスエラーを要求し割り当てることはできませんホスト名が問題ではありませんでしたポートにかかわらず、与えられたthroughingたローカル桟橋のサーバーインスタンスを作成するための責任new LocalServerReceiver.Builder()クラス内setHost()と呼ばれる機能がありますが。

全体の承認プロセスは、その主な機能のアプリへのアクセス権を与えるようにユーザーに要求しますURLを作成します

  1. を次のようにしているAuthorizationCodeInstalledAppクラスのAUTHORIZEの方法で行われます。
  2. 認証に成功すると、コードが受信されます(jettyサーバーのインスタンスは、コードが受信されるまで継続的にリッスンします)。
  3. オフラインアップロードのためにaccesstokenとrefreshtokenで受信したばかりのコードを交換します。
  4. Googleから受け取った認証情報をGoogleから保存します。

    1. を次のようにIは、元AuthorizationCodeInstalledAppを拡張する新しいクラスExtendedAuthorizationCodeInstalledAppを作成しclass.The方法における各機能のための各方法を作成したプロセス全体を分離する

    でありますgetAuthorizationFromStorage:ストアドクレデンシャルからアクセストークンを取得します。

  5. getAuthorizationFromGoogle:Googleからの認証情報を取得すると、ユーザーを認証ページに誘導するURLが作成され、状態パラメータにカスタム定義の名前と値のペアが作成されます。値はBase64エンコーダでエンコードする必要があります。認証後にGoogleからリダイレクトされた同じコードを受け取ることができます。
  6. saveAuthorizationFromGoogle:Googleから取得した認証情報を保存します。
    • は、応答が 認証後にGoogleからの受信 credentialDatastorfromからGoogleAuthorizationCodeFlowオブジェクトを作成します。
    • グーグルでいつでもリフレッシュトークンを取得して、 のアクセス権をいつでも取得することができます。
    • ストア ユーザーID

としてファイル名にaccesstokenとrefreshtokenのようなトークンは、コードの実装は、あなたの提案のためのhere

おかげ@KENdi ...

関連する問題