YouTube视频上传显示"必需参数:部分"某些用户的错误

时间:2018-03-17 15:17:25

标签: android youtube youtube-api youtube-data-api

我试图找出为什么我们的某些用户在尝试使用YouTube API v3将视频上传到YouTube时收到错误"Required parameter: part"。您可以在下面看到我们用于上传视频的代码。方法参数是有效的字符串,不会短或太长。

int shareYoutube(@NonNull Uri mediaUri, String mime, String mediaTitle, String postMessage, String accountName)
{
    int error = ERR_NO_ERROR;

    try {
        // Developer tags not supported yet
        //https://code.google.com/p/gdata-issues/issues/detail?id=5012

        // Authorize the request.
        GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(getApplicationContext(), Arrays.asList(YouTubeScopes.YOUTUBE_UPLOAD));
        credential.setSelectedAccountName(accountName);

        // This object is used to make YouTube Data API requests.
        YouTube.Builder builder = new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential);
        builder.setApplicationName("TEST APP");
        YouTube youtube = builder.build();

        // Add extra information to the video before uploading.
        Video video = new Video();

        // set privacy
        VideoStatus status = new VideoStatus();
        status.setPrivacyStatus("public");
        video.setStatus(status);

        // Most of the video's metadata is set on the VideoSnippet object.
        VideoSnippet snippet = new VideoSnippet();
        snippet.setTitle(mediaTitle);
        snippet.setDescription(postMessage);

        // Film & Animation https://gist.github.com/dgp/1b24bf2961521bd75d6c
        snippet.setCategoryId("1");

        // Set the keyword tags that you want to associate with the video.
        List<String> tags = new ArrayList<>();
        tags.add("animation");
        tags.add("cartoon");
        tags.add("2d animation");
        tags.add("drawing");
        snippet.setTags(tags);

        // Add the completed snippet object to the video resource.
        video.setSnippet(snippet);

        String fileFormat = "video/*";
        InputStreamContent mediaContent = new InputStreamContent(fileFormat, getContentResolver().openInputStream(mediaUri));

        // Insert the video. The command sends three arguments. The first
        // specifies which information the API request is setting and which
        // information the API response should return. The second argument
        // is the video resource that contains metadata about the new video.
        // The third argument is the actual video content.
        YouTube.Videos.Insert videoInsert = youtube.videos().insert("snippet,statistics,status", video, mediaContent);

        // Insert to a channel
        //videoInsert.setOnBehalfOfContentOwnerChannel();

        // Set the upload type and add an event listener.
        MediaHttpUploader uploader = videoInsert.getMediaHttpUploader();

        // Indicate whether direct media upload is enabled. A value of
        // "True" indicates that direct media upload is enabled and that
        // the entire media content will be uploaded in a single request.
        // A value of "False," which is the default, indicates that the
        // request will use the resumable media upload protocol, which
        // supports the ability to resume an upload operation after a
        // network interruption or other transmission failure, saving
        // time and bandwidth in the event of network failures.
        uploader.setDirectUploadEnabled(false);

        YouTubeCallbackListener callback = new YouTubeCallbackListener();
        uploader.setProgressListener(callback);

        // Call the API and upload the video.
        Video returnedVideo = videoInsert.execute();

        synchronized (callback) {
            if (!callback.hasCallbackResult()) {
                try {
                    callback.wait();
                    error = callback.error;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    error = ERR_POST_CANCELED;
                }
            } else {
                error = callback.error;
            }
        }

        if (ERR_NO_ERROR == error && null != returnedVideo)
        {
            Intent notificationIntent = new Intent(
                    Intent.ACTION_VIEW,
                    Uri.parse("http://www.youtube.com/watch?v="+returnedVideo.getId()));

            mPendingIntent = PendingIntent.getActivity(getBaseContext(), 0, notificationIntent, 0);
        }

    }
    catch (UserRecoverableAuthIOException userRecoverableException)
    {
        error = ERR_UNABLE_TO_AUTH_ACCOUNT;
        try {
            GoogleAuthUtil.getTokenWithNotification(
                    getApplicationContext(), accountName, "oauth2:" + YouTubeScopes.YOUTUBE_UPLOAD, null);
        } catch (IOException e) {
        } catch (GoogleAuthException e) {
        }
    } catch (GoogleJsonResponseException e) {
        error = ERR_POST_ERROR;
        Crashlytics.logException(e);
    } catch (IOException e) {
        error = ERR_POST_ERROR;
        Crashlytics.logException(e);
    } catch (Throwable t) {
        error = ERR_POST_ERROR;
        Crashlytics.logException(t);
    }

    return error;
}

错误:

400 Bad Request { "errors" : [ { "domain" : "global", "reason" : "required", "message" : "Required parameter: part", "locationType" : "parameter", "location" : "part" } ], "code" : 400, "message" : "Required parameter: part" } 

关于这里可能发生什么的任何想法?

1 个答案:

答案 0 :(得分:1)

问题是我们的程序脚本。我们所要做的只是包括以下内容。

-keep class com.google.api.** { *; }