UserRecoverableAuthException

UserRecoverableAuthException

我正在尝试使用最新的youtube api上传视频,但始终出现此错误:引起原因: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 token 已过期,您需要再次与Google进行身份验证。捕获UserRecoverableAuthException并在Exception对象上调用getIntent以获取身份验证Intent。然后再次启动Activity进行身份验证。

示例代码-

try {
    // Your upload code
    ...
} catch (UserRecoverableAuthIOException e) {
    startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
}

10-08 09:19