UserRecoverableAuthException: NeedPermission in Youtube API v3 (android)

不羁岁月 提交于 2019-12-25 06:26:53

问题


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.

This is my code :

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);
    }
};

Thanks for your help


回答1:


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.

Example code -

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


来源:https://stackoverflow.com/questions/37432726/userrecoverableauthexception-needpermission-in-youtube-api-v3-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!