【问题标题】:YouTube API v3 with OAuth2: update and delete fail with "Insufficient Permission" error带有 OAuth2 的 YouTube API v3:更新和删除失败并出现“权限不足”错误
【发布时间】:2013-10-08 05:49:39
【问题描述】:

我正在尝试通过google-api-client (0.6.4) Ruby gem 使用YouTube API v3OAuth2 for authenticationupdatedelete 视频。但是,当我尝试执行这两个操作中的任何一个时,我会看到以下错误消息:

Google::APIClient::ClientError: Insufficient Permission

奇怪的是: 使用与updatedelete 完全相同的身份验证程序,我可以成功insert(上传),没问题!所以,我认为这不是我的身份验证设置的问题,而是我的代码中的其他地方。

我的读写scope 在任何这些操作中总是相同的:

https://www.googleapis.com/auth/youtube https://www.googleapis.com/auth/youtube.upload

根据 API 文档,以空格分隔的范围集应涵盖 insertupdatedelete 操作。

我的client_idclient_secretrefresh_token 对于所有这些操作也总是相同的——所以,这也不是问题,不是吗?请注意,我的程序在到期时会自动获得一个新的access_token,因此,我不认为这就是问题所在。

比较一下,这是我的insert(上传)代码的样子(这可行):

# Auth stuff, then...
@client.execute!(
  :api_method => @youtube.videos.insert,
  :body_object => body,
  :media => Google::APIClient::UploadIO.new(file_path, 'video/*'),
  :parameters => {
    'uploadType' => 'multipart',
    :part => body.keys.join(','),
  }
)

这是我的delete 代码的样子(失败):

# Auth stuff, then...
@client.execute!(
  :api_method => @youtube.videos.delete,
  :parameters => {
    'id' => youtube_id,
  }
)

我错过了什么?不幸的是,YouTube API documentation for delete 没有提供任何示例,所以我没有什么可比较的。如果我可以提供更多信息以使我的问题更清楚,请告诉我。

【问题讨论】:

    标签: ruby api youtube youtube-api google-api-ruby-client


    【解决方案1】:

    我相当确定这个问题的所有 11 个观点(在撰写本文时)都是我,但我将发布一个答案,以防将来对某人有所帮助:

    我的代码本身没有问题。问题出在我最初为此帐户创建 refresh_token 时。

    对于没有经验的人,YouTube 数据 API (v3) 不支持“服务帐户”,在 Google API 生态系统的其他地方,当您是唯一的客户端时,通常会采用这种方式来设置 OAuth2 身份验证客户端是你自己。解决方法是您必须手动。采取以下步骤:


    首先,转到 Google“API 控制台”。在那里,在“API 访问”下,您需要为“已安装的应用程序”“创建客户端 ID”。这会给你一个Client ID、一个Client secret和一个Redirect URI(你会想要一个非localhost)。把这些写下来。

    接下来,您需要手动获取授权码,方法是在您喜欢的网络浏览器中访问如下所示的 URL,同时登录到您刚刚为其创建客户端 ID 的同一帐户

    https://accounts.google.com/o/oauth2/auth
      ?client_id={client_id}
      &redirect_uri={redirect_uri}
      &scope={space separated scopes}
      &response_type=code
      &access_type=offline
    

    当然需要输入client_idredirect_uriscope查询参数。就我而言,这是我出错的地方。当我执行此手动步骤时,我应该将 scope 参数设置为:

    https://www.googleapis.com/auth/youtube https://www.googleapis.com/auth/youtube.upload
    

    但我只是做了https://www.googleapis.com/auth/youtube.upload,这不足以更新/删除视频!

    最后,您需要获取refresh_token,通过这样的 URL:

    https://accounts.google.com/o/oauth2/token
      ?code={authorization_code}
      &client_id={client_id}
      &client_secret={client_secret}
      &redirect_uri={redirect_uri}
      &grant_type=authorization_code
    

    然后用如下命令卷曲它:

    $ curl https://accounts.google.com/o/oauth2/token -d "code=..."
    

    这将返回一个 JSON 响应,其中包含您的 refresh_token,然后您在通过 Google API 以编程方式授权您的请求时使用该响应。

    【讨论】:

    猜你喜欢
    • 2019-08-04
    • 2015-12-09
    • 2019-07-29
    • 2015-08-29
    • 2022-08-19
    • 2021-01-18
    • 2015-07-01
    • 2016-06-20
    • 1970-01-01
    相关资源
    最近更新 更多