问题描述
我正在尝试使用最新的youtube api上传视频,但始终出现此错误:引起原因:com.google.android.gms.auth.UserRecoverableAuthException:NeedPermission.
i'm trying to upload a video with the last youtube api but all the time I get this error : Caused by: com.google.android.gms.auth.UserRecoverableAuthException: NeedPermission.
这是我的代码:
AsyncTask<Void, Void, Void> youtubeUploadTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
YouTube youtube;
/**
* Define a global variable that specifies the MIME type of the video
* being uploaded.
*/
String VIDEO_FILE_FORMAT = "video/*";
String SAMPLE_VIDEO_FILENAME = "sample-video.mp4";
try {
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, mActivity.credential).setApplicationName(
mActivity.getResources().getString(R.string.app_name)).build();
System.out.println("Uploading: " + SAMPLE_VIDEO_FILENAME);
// Add extra information to the video before uploading.
Video videoObjectDefiningMetadata = new Video();
VideoStatus status = new VideoStatus();
status.setPrivacyStatus("public");
videoObjectDefiningMetadata.setStatus(status);
// Most of the video's metadata is set on the VideoSnippet object.
VideoSnippet snippet = new VideoSnippet();
Calendar cal = Calendar.getInstance();
snippet.setTitle("Test Upload via Java on " + cal.getTime());
snippet.setDescription(
"Video uploaded via YouTube Data API V3 using the Java library " + "on " + cal.getTime());
// Set the keyword tags that you want to associate with the video.
List<String> tags = new ArrayList<>();
tags.add("test");
tags.add("example");
tags.add("java");
tags.add("YouTube Data API V3");
tags.add("erase me");
snippet.setTags(tags);
// Add the completed snippet object to the video resource.
videoObjectDefiningMetadata.setSnippet(snippet);
File mediaFile = new File(filePath.toString());
if (mediaFile.isFile()) {
InputStreamContent mediaContent = new InputStreamContent(VIDEO_FILE_FORMAT,
new BufferedInputStream(new FileInputStream(mediaFile)));
YouTube.Videos.Insert videoInsert = youtube.videos()
.insert("snippet,statistics,status", videoObjectDefiningMetadata, mediaContent);
// Set the upload type and add an event listener.
MediaHttpUploader uploader = videoInsert.getMediaHttpUploader();
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);
// Call the API and upload the video.
Video returnedVideo = videoInsert.execute();
// Print data about the newly inserted video from the API response.
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();
}
return null ;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}
};
感谢您的帮助
推荐答案
我认为auth令牌已过期,您需要再次与Google进行身份验证.捕获 UserRecoverableAuthException
并在Exception对象上调用 getIntent
以获取身份验证 Intent
.然后再次启动活动
进行身份验证.
I think the auth token expired and you need to authenticate with Google again. Catch the UserRecoverableAuthException
and call getIntent
on the Exception object to get the authentication Intent
. Then start Activity
for authentication again.
示例代码-
try {
// Your upload code
...
} catch (UserRecoverableAuthIOException e) {
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
}
这篇关于UserRecoverableAuthException:Youtube API v3(android)中的NeedPermission的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!