Google Oauth for YouTube : Why do I get “Undefined

2020-05-09 17:01发布

问题:

I got a little problem with the OAuth for Google, I use the Jim Saunder Libraire for CodeIgniter (http://codeigniter.com/wiki/OAuth_for_Google) but when I came to my access_youtube function, which look like that :

public function access_youtube()
{ //STEP 2

// Here is the first call to load the library 
    $params['key'] = 's390075769.onlinehome.fr';
    $params['secret'] = 'iHD72YKzWmbm8VTwncht_E-d';

// We  can change the signing algorithm and http method by setting the following in your params array.
    $params['algorithm'] = 'HMAC-SHA1';
    // $params['method'] = "GET";

// We need to reload the library since we left our site beetween Step 1 & 2.
    $this->load->library('google_oauth', $params);

// We are using HMAC signing you need to specify the token_secret you got from the request step as the second parameter of this method. So
    $token_secret = $this->session->userdata('token_secret');
    $oauth = $this->google_oauth->get_access_token(false, $token_secret);

// The access method will return an array with keys of ‘oauth_token’, and ‘oauth_token_secret’ 
// These values are your access token and your access token secret. We should store these in our database.  
    $this->session->set_userdata('oauth_token', $oauth['oauth_token']);
    $this->session->set_userdata('oauth_token_secret', $oauth['oauth_token_secret']);

// Now you have an access token and can make google service requests on your users behalf
    redirect(site_url());
}

I got several error messages :

Undefined index: oauth_token

Undefined index: oauth_token_secret

which refer to the lines $this->session->set_userdata ...

and, after each error message I get :

Message: Cannot modify header information - headers already sent by (output started at /homepages/7/d390075755/htdocs/system/core/Exceptions.php:170) Filename: libraries/Session.php Line Number: 671

which, I think is linked with the previous error message.

so I tried that in the constructor :

class Example extends CI_Controller
{
private $CI;
public function __construct()
{
     parent::__construct();

    $this->CI =& get_instance();
    $this->CI->load->library('session');

    $oauth = array();
    $oauth['oauth_token_secret']='';
    $oauth['oauth_token']='';

}

just before my function but it doesn't do anything .. and I'm affraid I'm gonna loose my session variables stock by google ... isn't it ? Do I get my tokens from google ?

回答1:

Honestly i am not sure how Session will be managed by PHP code so will be of little help for you. It seems that this line is the cause

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

you are trying to get access_token from the Google to send request to Google API to get user information who ever authorize your application and i guess Google is not returning what has been expected from them. this can be due to type mismatch in the request data as what Google Oauth is expecting from you and what actually you are sending

In you case i would have used debugger of my Eclipse to see what exactly is going on but for OAuth i already told you in earlier question

  1. You need to store that token you getting at your first step.
  2. Once your user is back from authenticate them-self you need to get access_token using that request token
  3. Once you have request token you are ready to make final API call and it seems that some how your code is failing to get the data from session