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;
}
選中此[SO問題](http://stackoverflow.com/questions/31737699/upload-videos-to-youtube-from-my-web-server-in-java)如果它可以幫助你。 – KENdi
我已經通過寫延伸的谷歌的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) –
這是更好,如果你張貼爲答案:) – KENdi