私は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;
}
チェックこれで[SO質問](http://stackoverflow.com/questions/31737699/upload-videos-to-youtube-from-my-web-server-in-java)を参照してください。 – KENdi
は、私は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) –
あなたが答えとして投稿した方が良いです:) – KENdi