HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(jsonFactory).setClientSecrets(CLIENT_ID, CLIENT_SECRET).addRefreshListener(new CredentialRefreshListener() {
@Override
public void onTokenResponse(Credential credential, TokenResponse tokenResponse) {
// Handle success.
System.out.println("Credential was refreshed successfully.");
}
@Override
public void onTokenErrorResponse(Credential credential,
TokenErrorResponse tokenErrorResponse) {
// Handle error.
System.err.println("Credential was not refreshed successfully. "
+ "Redirect to error page or login screen.");
}
})
.build();;
credential.setAccessToken(pushOvpRequest.getConnectorYoutube().getAccessToken());
credential.setRefreshToken(pushOvpRequest.getConnectorYoutube().getRefreshToken());
System.out.println("refresh token = " + pushOvpRequest.getConnectorYoutube().getRefreshToken());
// YouTube object used to make all API requests.
youtube = new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName("X").build();
System.out.println("check = " + credential.getAccessToken());
// We get the user selected local video file to upload.
File videoFile = getVideoFromUser();
System.out.println("You chose " + videoFile + " to upload.");
// Add extra information to the video before uploading.
Video videoObjectDefiningMetadata = new Video();
/*
* Set the video to public, so it is available to everyone (what
* most people want). This is actually the default, but I wanted you
* to see what it looked like in case you need to set it to
* "unlisted" or "private" via API.
*/
VideoStatus status = new VideoStatus();
status.setPrivacyStatus(pushOvpRequest.getPrivacyStatus());
videoObjectDefiningMetadata.setStatus(status);
// We set a majority of the metadata with the VideoSnippet object.
VideoSnippet snippet = new VideoSnippet();
/*
* The Calendar instance is used to create a unique name and
* description for test purposes, so you can see multiple files
* being uploaded. You will want to remove this from your project
* and use your own standard names.
*/
snippet.setTitle(pushOvpRequest.getTitle());
snippet.setDescription(pushOvpRequest.getDescription());
// Set your keywords.
// TODO set tags
List<String> tags = new ArrayList<String>();
tags.add("test");
tags.add("example");
tags.add("java");
tags.add("YouTube Data API V3");
tags.add("erase me");
snippet.setTags(tags);
// Set completed snippet to the video object.
videoObjectDefiningMetadata.setSnippet(snippet);
InputStreamContent mediaContent = new InputStreamContent(VIDEO_FILE_FORMAT, new BufferedInputStream(new FileInputStream(videoFile)));
mediaContent.setLength(videoFile.length());
/*
* The upload command includes: 1. Information we want returned
* after file is successfully uploaded. 2. Metadata we want
* associated with the uploaded video. 3. Video file itself.
*/
YouTube.Videos.Insert videoInsert = youtube.videos().insert("snippet,statistics,status", videoObjectDefiningMetadata, mediaContent);
// Set the upload type and add event listener.
MediaHttpUploader uploader = videoInsert.getMediaHttpUploader();
/*
* Sets whether direct media upload is enabled or disabled. True =
* whole media content is uploaded in a single request. False
* (default) = resumable media upload protocol to upload in data
* chunks.
*/
uploader.setDirectUploadEnabled(false);
MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() {
public void progressChanged(MediaHttpUploader uploader) throws IOException {
switch (uploader.getUploadState()) {
case INITIATION_STARTED:
System.out.println("Initiation Started");
break;
case INITIATION_COMPLETE:
System.out.println("Initiation Completed");
break;
case MEDIA_IN_PROGRESS:
System.out.println("Upload in progress");
System.out.println("Upload percentage: " + uploader.getProgress());
break;
case MEDIA_COMPLETE:
System.out.println("Upload Completed!");
break;
case NOT_STARTED:
System.out.println("Upload Not Started!");
break;
}
}
};
uploader.setProgressListener(progressListener);
// Execute upload.
Video returnedVideo = videoInsert.execute();
// Print out returned results.
System.out.println("\n================== Returned Video ==================\n");
System.out.println(" - Id: " + returnedVideo.getId());
System.out.println(" - Title: " + returnedVideo.getSnippet().getTitle());
System.out.println(" - Tags: " + returnedVideo.getSnippet().getTags());
System.out.println(" - Privacy Status: " + returnedVideo.getStatus().getPrivacyStatus());
System.out.println(" - Video Count: " + returnedVideo.getStatistics().getViewCount());
} catch (GoogleJsonResponseException e) {
System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : " + e.getDetails().getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("IOException: " + e.getMessage());
e.printStackTrace();
} catch (Throwable t) {
System.err.println("Throwable: " + t.getMessage());
t.printStackTrace();
}
我在控制台中出现以下错误:
Throwable:null java.lang.NullPointerException at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:191) 在 com.google.api.client.json.jackson2.JacksonFactory.createJsonParser(JacksonFactory.java:78) 在 com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:75) 在 com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:71) 在 com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:88) 在 com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:303) 在 com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:323) 在 com.google.api.client.auth.oauth2.Credential.executeRefreshToken(Credential.java:586) 在 com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:300) 在 com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:505) 在 com.google.api.client.auth.oauth2.Credential.handleResponse(Credential.java:292) 在 com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1041) 在 com.google.api.client.googleapis.media.MediaHttpUploader.executeUploadInitiation(MediaHttpUploader.java:436) 在 com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:327) 在 com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:415) 在 com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:340) 在 com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:458) 在ProcessPush.processUpload(ProcessPush.java:257)at ProcessPush.push(ProcessPush.java:129)at PushOvpRequestHandlerThread.run(PushOvpRequestHandlerThread.java:43)
这段代码昨天正在运行,但它今天早上就停止工作了!
答案 0 :(得分:1)
这不是你的代码问题,而是他们的服务器。我也有同样的问题,它从今天早上开始,直到那时它起作用了。即使使用新的参考文献也没有帮助我,我花了一整天时间试图解决它。此外,找不到支持,看看他们是否知道什么时候会被修复。
请参阅:https://code.google.com/p/gdata-issues/issues/detail?id=6354