YouTube API Request : How could I get the Right to

2019-05-22 23:50发布

问题:

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

and here : http://codeigniter.com/wiki/OAuth_for_Google/ (approximately the same)

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 :

The Url I got is :

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

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 :

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

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

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 ?

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

  • -Message: Undefined index: oauth_token
  • -Message: Undefined index: oauth_token_secret

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 ?

Thank You :)

回答1:

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

  1. oauth_verifier
  2. oauth_token

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 

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

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

Token token=authGetRequestToken.getRequestToken(service);

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);

and once you have this accessToken you are happy to go

hope it make sense

edit1

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.

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']);

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

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);

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

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

hope its clear for you