0

目标是为网络应用程序拥有一个 Google (YouTube) 帐户。该网络应用程序的用户将能够通过此帐户将视频上传到一个 YouTube 频道。几个小时后,我终于结束了。我已经找到了大量示例如何实现 Google 用户 <-> 网络应用交互,但我不需要如此全面的解决方案。

我正在尝试 OAuth 2.0(按照推荐)和 Google API Ruby 客户端(https://github.com/google/google-api-ruby-client

到目前为止,我已经通过网络应用程序授权了 Google 帐户(将拥有那个 YouTube 频道),包括所有必要的范围、离线访问,并且我有刷新访问令牌的机制。所以我有访问令牌、刷新令牌、客户端 ID 和客户端密码。

但我不知道如何发送一个简单的授权请求。下面的结果返回“超出未经验证使用的每日限制”。过了一会儿,出了点问题-我想我缺少客户端ID和客户端密码的部分。

所以问题是:当我们只与一个用户合作并且我们拥有所有必要的 ID、秘密和令牌时,如何通过 OAuth 2.0 使用 Google API Ruby 客户端发送简单的授权请求?

感谢您的任何帮助或建议。

# Faraday connection
conn = Faraday.new(:url => 'https://accounts.google.com',:ssl => {:verify => false}) do |faraday|
  faraday.request  :url_encoded
  faraday.response :logger
  faraday.adapter  Faraday.default_adapter
end    

# Refresh token
result = conn.post '/o/oauth2/token', {
  'refresh_token' => "1/1lDIvifN******************dk9Akuc9ELVKM0",
  'client_id' => "61********506.apps.googleusercontent.com",
  'client_secret' => "********************g_dLfKmi",
  'grant_type' => 'refresh_token'}

@output = ActiveSupport::JSON.decode result.body
@access_token = @output['access_token']   
@token_type = @output['token_type'] 


# Google Client
client = Google::APIClient.new      

# YouTube API v3
api = client.discovered_api('youtube', 'v3')

# Retrieve list of playlists (not working)
@result = client.execute(
  :api_method => api.playlists.list,
  :parameters => {'part' => 'snippet', 'mine' => 'true'},
  :authorization => {'token_type' => @token_type, 'access_token' => @access_token}
)
4

1 回答 1

0

好的,所以我虽然执行请求中的 :authorization 参数会添加 HTTP 标头 Authorization: token_type access_token 本身,但不是,这是一个问题。

所以这有效:

@result = client.execute(
  :api_method => api.playlists.list,
  :parameters => {'part' => 'snippet', 'mine' => 'true'},
  :authorization => {:token_type => @token_type, :access_token => @access_token},
  :headers => {:authorization => @token_type + ' ' + @access_token}
)
于 2014-02-01T10:27:43.213 回答