2

我正在尝试设置一项服务来帮助管理用户的 youtube 帐户,包括即使在用户未登录的情况下也可以预定的视频上传。我在身份验证方面遇到了一些奇怪的问题,我似乎找不到方法要解决这个问题。

我的omniauth初始化程序:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET'],
    {
      name: 'google',
      scope: 'http://gdata.youtube.com, email, profile, plus.me, youtube, youtube.upload',
      prompt: 'consent',
      access_type: 'offline',
      image_aspect_ratio: 'square
    }
end

我的授权代码首当其冲:

YOUTUBE_SCOPE = 'https://www.googleapis.com/auth/youtube https://www.googleapis.com/auth/youtube.upload'

account = User.find(1234)
client = Google::APIClient.new(application_name: 'YouTube Delivery',
                               application_version: '1.0.0')
api = client.discovered_api('youtube', 'v3')

client.authorization.client_id = ENV['GOOGLE_CLIENT_ID']
client.authorization.client_secret = ENV['GOOGLE_CLIENT_SECRET']
client.authorization.grant_type = 'refresh_token'
client.authorization.refresh_token = account.refresh_token
client.authorization.scope = YOUTUBE_SCOPE
client.authorization.fetch_access_token! if client.authorization.expired?

所有这些代码似乎都有效。用户可以登录,并且我已验证是否请求了适当的权限。还会提示用户给予离线同意。我取回了一个刷新令牌,当当前令牌过期时,我可以使用刷新令牌获取新的访问令牌。那里没有问题。

当我在用户不存在时尝试将视频上传到 youtube 时出现问题:

video = Video.find(1234)
request_body = {
  snippet: {
    title: video.title,
    description: video.youtube_description,
    tags: video.tags
  },
  status: { privacyStatus: 'private' }
}

response = client.execute!(
  api_method: api.videos.insert,
  body_object: request_body,
  media: Google::APIClient::UploadIO.new(video.local_path, 'video/*'),
  parameters: {
    uploadType: 'resumable',
    part: 'snippet,status'
  },
  authorization: client.authorization
)

这是谷歌回击的地方Google::APIClient::AuthorizationError: Unauthorized

根据谷歌文档,我可以通过运行测试我拥有的访问令牌:

curl https://www.googleapis.com/youtube/v3/channels?part=id&mine=true&access_token=ACCESS_TOKEN

对我来说,这会返回:

 { "error": {
   "errors": [
     {
       "domain": "usageLimits",
       "reason": "dailyLimitExceededUnreg",
       "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
       "extendedHelp": "https://code.google.com/apis/console"
     }
   ],
   "code": 403,
   "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
 }}

我已确保已在 google api 控制台中将以下 api 添加到我的项目中:联系人、google+、youtube 数据、youtube 分析。我还检查了我的所有环境变量是否都已正确设置。

不太确定从这里去哪里。任何指导将不胜感激!

我的导轨版本:4.1.6

4

0 回答 0