Google API Ruby客户端 - 具有OAuth 2.0的单个用户 [英] Google API Ruby Client - single user with OAuth 2.0

查看:153
本文介绍了Google API Ruby客户端 - 具有OAuth 2.0的单个用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



到目前为止,我已通过网络应用授权Google帐户(将拥有该YouTube频道) ,包括所有必要的作用域,离线访问以及我有刷新访问令牌的机制。所以我有访问令牌,刷新令牌,客户端ID和客户端密钥。



但我不知道如何发送简单的授权请求。下面的结果返回给我每日限制超出未经验证的使用。过了一段时间,所以错了 - 我想我缺少与客户端ID和客户端秘密的一部分。所以问题是:如何通过OAuth 2.0通过Google API Ruby Client发送简单的授权请求,当我们只与一个用户合作,并且我们拥有所有必要的ID,秘密和标记?



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

 #法拉第连接
conn = Faraday.new(:url =>'https://accounts.google.com',::ssl => {:verify => false})做法拉第|
faraday.request:url_encoded
faraday.response:记录器
faraday.adapter Faraday.default_adapter
结束

#刷新令牌
结果= conn.post'/ o / oauth2 / token',{
'refresh_token'=> 1 / 1DIvifN ****************** 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客户端
客户端= Google :: APIClient.new

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

#检索播放列表(不工作)
@result = client.execute(
:api_method => api.playlists。
:authorization => {'token_type'=> {'part'=>'snippet','mine'=>'true'}, token_type,'access_token'=> @access_token}


解决方案<所以我通过执行请求中的授权参数来添加HTTP头Authorization:token_type access_token本身,但没有,这是一个问题。



所以这是有效的:

  @res ult = client.execute(
:api_method => api.playlists.list,
:parameters => {'part'=> 'snippet','mine'=> 'true'},
:authorization => {:token_type => @token_type,:access_token => @access_token},
:headers => {:授权=> @token_type +''+ @access_token}


the goal is to have one Google (YouTube) account for the web app. Users of the web app will be able to upload videos via this account to the one YouTube channel. After many hours im in the ends. I've found plenty of samples how to implement for Google user <-> web app interaction, but I don't need such comprehensive solution.

I'm trying over OAuth 2.0 (as recommended) and with Google API Ruby Client (https://github.com/google/google-api-ruby-client)

So far I have authorized the Google account (which will have that YouTube channel) with the web app, all necessary scopes included, offline access too and I have mechanism for refreshing access token. So I have access token, refresh token, client id and client secret.

But I don't know how to send a simple authorized request. The result below returns me "Daily Limit for Unauthenticated Use Exceeded." after a while so something wrong - i guess im missing part with client id and client secret.

So the question is: How to send simply authorized request via OAuth 2.0 with Google API Ruby Client, when we work with only one user and we have all necessary ids, secrets and tokens?

Thanks for any help or suggestion.

# 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}
)

解决方案

Ok, so I though the :authorization param in the execute request will add HTTP header Authorization: token_type access_token itself, but not and it was a problem.

So this works:

@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}
)

这篇关于Google API Ruby客户端 - 具有OAuth 2.0的单个用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆