YouTube API 请求:我怎样才能获得正确的令牌(关于 Jim S. 的代码) [英] YouTube API Request : How could I get the Right token ( about Jim S.' code )

查看:18
本文介绍了YouTube API 请求:我怎样才能获得正确的令牌(关于 Jim S. 的代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 codeIgniter 网站上实现 YouTube API.我按照 Jim Saunders 说明 操作:

I’m trying to implement the YouTube API on my codeIgniter website. I followed the Jim Saunders instructions here :

这里:http://codeigniter.com/wiki/OAuth_for_Google/(大致相同)

所以首先,我需要使用 request_youtube 函数获取令牌密钥,该函数可以正常工作,我已经从谷歌输入了我的消费者密钥和消费者秘密,一切都很好.然后我被重定向到 YouTube 以授权访问 YouTube,但是当我被重定向到我的网站时,我遇到了两个问题:

SO first of all, I needed to get the token secret with a request_youtube function, that function works correctly, i’ve entered my consumer key and consumer secret from google and everything is just fine. So then I’m redirected to YouTube to authorize the YouTube access, but when I’m redirected to my site, I got 2 problems :

我得到的网址是:

mywebsite/example/access_youtube=&oauth_verifier=6wbPcxlOAmYQwaDU7HIhz38B&oauth_token=1/6Nfchy_nM-j4vxQzhAWTuc1J20L02bBNdcZasQP-e0s

codeIgniter 不会理解 /" 并且不会将我重定向到 example/access_youtube 函数,如果我更正手动,然后我使用我的参数转到我的 access_youtube 函数,但这是我遇到的第二个(致命)问题:

and codeIgniter doesn’t understant the "/" and doesn’t redirect me to the example/access_youtube function, If I correct it manually, then I go to my access_youtube function with my parameters but here is second (fatal) problem I got :

首先,这是我从 url 中得到的两个变量:

First, here are my two variables I got from the url :

oauth_verifier=6wbPcxlOA************
&
oauth_token=1/6Nfchy_nM-j4vxQzhAWTuc1J20L02b***********

但是,在 google Oauth Playground(您可以在这里找到:http://googlecodesamples.com/oauth_playground/)我应该得到类似的东西:

yet, on the google Oauth Playground (that you’ll be able to find here : http://googlecodesamples.com/oauth_playground/) I’m supposed to get something like that :

oauth_token=1/P9o-yO1Czz1q67pvPm**********************iMI&
oauth_token_secret=CzeSat342hO5HzswJEq94ZPH&oauth_callback_confirmed=true

所以这意味着我没有得到相同的变量......为什么?我怎么能得到那些变量而不是我得到的变量?它们是一样的吗?

so that mean I didn’t get the same variables ... why ? how could I get thoses variables instead of the one I got ? Are they the same ?

第二,我的 access_Youtube 函数中有几个错误,CI 告诉 :

Second, I got several errors in my access_Youtube function, CI tell :

  • -消息:未定义索引:oauth_token
  • -消息:未定义索引:oauth_token_secret

所以它没有从标题中获取信息......我真的不知道该怎么做,我已经按照吉姆的说明进行操作,但我有点卡在那里,有没有人尝试过在他的网站上实现 OAuth 访问有什么想法吗?

so it doesn’t get informations from the header ... I don’t really know how to do so, I’ve followed the Jim’ Instructions but I’m a kind of stuck there, does anyone who tried to implement an OAuth access on his site has an idea ?

谢谢:)

推荐答案

好的,直到你从 google 重定向回来之前你正在做的事情是完全正确的.

Ok what you are doing till you get redirect back from google is perfectly correct.

  1. oauth_verifier
  2. oauth_token

oauth_verifier 代码是谷歌在用户授权您的应用程序访问他/她的个人资料时发送给您的代码,因此您需要此验证程序来获取带有访问令牌的 AccessToken,您将能够访问用户个人资料/注明您定义的范围.

oauth_verifier code is one google send you back if user authorize your application to access his/her profile so you need this verifier to get AccessToken with access token you will able to get access to user profile/date what ever scope you have defined.

oauth_token 

这与您的申请相关联,Google 会以此识别您,您的申请将保持不变

this is associated with your application and Google identify you with this it will remain same for your aplication

所以当你通过调用类似的东西从 google/you Tube API 获取 AccessToken

so when you are get AccessToken from google/you tube API by calling something like

Token token=authGetRequestToken.getRequestToken(service);

此请求令牌包含您的秘密,因此在将用户重定向到 youtube 之前,您需要将此令牌保存到数据库或会话中.一旦用户重定向回您的应用程序,您需要在使用此令牌后取回此令牌验证码连同此令牌以获得 Access_token 之类的

this request token contains your secret, so before redirecting user to youtube you need to persist this Token either to database or to the session.Once user redirected back to your application you need to get back this token once you have this token use the verification_code along with this token to get Access_token like

Token accessToken = authGetAccessToken.getAccessToken((Token)   
 session.get(OAuthConstants.REQUEST_TOKEN), verifier);

一旦你有了这个accessToken,你就很高兴了

and once you have this accessToken you are happy to go

希望有意义

edit1

OAuth 系统正在发送 Verification_code,以表明用户已授予访问权限并已自行授权.这只是握手机制的一部分.

Verification_code is being send by the OAuth system to indicate that user has given access and has authorize there self. This is only a part of handshaking mechanism.

我浏览了 OP 在这里提到的页面就是那里提到的内容

i gone through the page OP has mentioned here is what has been mentioned there

$response = $this->google_oauth->get_request_token(site_url("/user/youtube_access"));
$this->_store_somewhere($response['token_secret']);

所以它被要求将 token_secret 存储在某个地方,您可以选择 DB 或 session 以您选择的任何方式,因为此 token_secret 将用于重建请求后期再来

So it is being asked to store token_secret at some place, you can choose DB or session what ever way you choose as this token_secret will be used to reconstruct the request again at later stage

在我的代码示例中,我保存了包含 token_secretoauth_token

in my code example i have saved whole Request_Token which contains token_secret and oauth_token

一旦用户被重定向回您的应用程序,您需要执行以下步骤

once user is redirected back to your application you need to perform the following steps

$token_secret = $this->_get_from_storage('token_secret');
 $oauth = $this->google_oauth->get_access_token(false, $token_secret);

这里我们正在检索我们在上一步中保存的 token_secret,然后发送请求 Google 提供 access_token

Here we are retrieving the token_secret we saved in last step and than sending asking Google to provide access_token

我也在做几乎同样的事情,将验证器和 token_secret 一起传递

i am also doing almost same thing passing verifier along with token_secret

希望对你来说很清楚

这篇关于YouTube API 请求:我怎样才能获得正确的令牌(关于 Jim S. 的代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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