【问题标题】:Regaining revoked access to YouTube account重新获得对 YouTube 帐户的撤销访问权限
【发布时间】:2017-10-21 13:58:13
【问题描述】:

我创建了一个应用程序来将某些自定义缩略图上传到 YouTube 视频,它工作正常,但是我撤销了从我的 YouTube 帐户对我的应用程序的访问权限以测试某些内容,并且我的应用程序不再请求访问权限。现在,每当我尝试使用我的应用程序时,它只会响应:

 IOException: 400 Bad Request
 {
   "error" : "invalid_grant",
   "error_description" : "Token has been expired or revoked."
 }
 com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
 {
   "error" : "invalid_grant",
   "error_description" : "Token has been expired or revoked."
 }

我使用了 YouTube 页面中的缩略图集示例:https://developers.google.com/youtube/v3/docs/thumbnails/set

我想知道我可以在以下代码中进行哪些更改以使登录页面再次显示,以便我可以再次访问我的应用程序,我的代码在这里:

private void uploadThumbnail(String videoId, BufferedImage thumbnail){
    YouTube youtube;
    String IMAGE_FILE_FORMAT = "image/png";
    // This OAuth 2.0 access scope allows for full read/write access to the
    // authenticated user's account.
    ArrayList<String> scopes = new ArrayList<>();
    scopes.add("https://www.googleapis.com/auth/youtube");

    try {
        // Authorize the request.
        Credential credential = Auth.authorize(scopes, "uploadthumbnail");

        // This object is used to make YouTube Data API requests.
        youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName(
                "youtube-cmdline-uploadthumbnail-sample").build();

        get.saveImg(thumbnail,"./Screens/screenshot0t.png");
        File imageFile = new File("./Screens/screenshot0t.png");

        // Create an object that contains the thumbnail image file's
        // contents.
        InputStreamContent mediaContent = new InputStreamContent(
                IMAGE_FILE_FORMAT, new BufferedInputStream(new FileInputStream(imageFile)));
        mediaContent.setLength(imageFile.length());

        // Create an API request that specifies that the mediaContent
        // object is the thumbnail of the specified video.
        Set thumbnailSet = youtube.thumbnails().set(videoId, mediaContent);

        // Set the upload type and add an event listener.
        MediaHttpUploader uploader = thumbnailSet.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);

        // Set the upload state for the thumbnail image.
        MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() {
            @Override
            public void progressChanged(MediaHttpUploader uploader) throws IOException {
                switch (uploader.getUploadState()) {
                    // This value is set before the initiation request is
                    // sent.
                    case INITIATION_STARTED:
                        System.out.println("Initiation Started");
                        break;
                    // This value is set after the initiation request
                    //  completes.
                    case INITIATION_COMPLETE:
                        System.out.println("Initiation Completed");
                        break;
                    // This value is set after a media file chunk is
                    // uploaded.
                    case MEDIA_IN_PROGRESS:
                        System.out.println("Upload in progress");
                        System.out.println("Upload percentage: " + uploader.getProgress());
                        break;
                    // This value is set after the entire media file has
                    //  been successfully uploaded.
                    case MEDIA_COMPLETE:
                        System.out.println("Upload Completed!");
                        break;
                    // This value indicates that the upload process has
                    //  not started yet.
                    case NOT_STARTED:
                        System.out.println("Upload Not Started!");
                        break;
                }
            }
        };
        uploader.setProgressListener(progressListener);

        // Upload the image and set it as the specified video's thumbnail.
        ThumbnailSetResponse setResponse = thumbnailSet.execute();

        // Print the URL for the updated video's thumbnail image.
        System.out.println("\n================== Uploaded Thumbnail ==================\n");
        System.out.println("  - Url: " + setResponse.getItems().get(0).getDefault().getUrl());
        get.deleteFile(imageFile.getPath());
    } 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();
    }
}

【问题讨论】:

  • 获取另一个令牌!你杀了那个人。
  • 我该怎么做呢?抱歉,我刚开始使用 oauth
  • 您撤销了访问权限,您需要再次请求访问权限。还记得我弹出来要求您授予访问该帐户的权限吗?
  • 是的,这就是我想知道的,我可以使用什么功能使该屏幕再次出现?是不是类似于 credential.getClientAuthentication()?
  • 我认为您必须一直回到 credential = Auth.authorize(scopes, "uploadthumbnail"); - 是对 authorize 的调用创建了凭据。

标签: java youtube-api access-token youtube-data-api


【解决方案1】:

我在使用 Gmail API 时遇到了完全相同的问题,删除存储的凭据后一切恢复正常。

例如rm ~/.credentials/gmail-java-quickstart/StoredCredential

【讨论】:

    猜你喜欢
    • 2021-04-15
    • 2021-05-11
    • 2013-04-20
    • 2012-07-30
    • 2019-05-12
    • 1970-01-01
    • 2022-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多