1

Recently, I'm developing my site with social networking, and want to import gmail,yahoo,hotmail etc.. contacts.

I have already grabbed contacts from Gmail and now next want to do the same with Yahoo. Reading the Yahoo documentation and following my gmail code, I get the authorisation screen and get the auth code back, but unable to get the access token. (I am using curl and I am expecting help related to it).

Here is the dump: string

'code=hzbp3px&client_id=dj0yJmk9WlJ0a3dmQTQ3ZFcyJmQ9WVdrOVFXRXliSFpMTm1zbWNHbzlNQS0tJnM9Y29uc3VtZXJzZWNyZXQmeD1jZA--&client_secret=5b3a803684538bae27519af3e34f3e268da432a1&redirect_uri=http://localhost/DEVELOPMENT/Grabber.php&grant_type=authorization_code' (length=254)

object(stdClass)[5] public 'error' => string 'invalid_request' (length=15)

Here is my functions:

public function get_url(){
    global $session;        
    switch ($session->get_provider()) {
        case "gmail": return "https://accounts.google.com/o/oauth2/token"; 
        break;
        case "yahoo": return "https://api.login.yahoo.com/oauth2/get_token"; 
        break;
    }
}

   public function curl_query($post,$boolean){
      $curl = curl_init();
      curl_setopt($curl,CURLOPT_URL,$this->get_url());
      curl_setopt($curl,CURLOPT_POST,true);
      curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
      curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
      curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
      $result = curl_exec($curl);
      curl_close($curl);


      $response =  json_decode($result);
      var_dump($response);
      //exit;

      switch ($boolean) {
         case "true": return $response->access_token;   
         case "false": return $response->refresh_token;
      }         
   }

   public function get_access_token($auth_code){
      global $session;
      switch ($session->get_provider()) {
         case "gmail": return $this->curl_query($this->post_params($auth_code,'access_token'),true);
         case "yahoo": 
         $accesstoken = $this->curl_query($this->post_params($auth_code,'access_token'),true);
         //$refreshtoken = $this->curl_query($this->post_params($accesstoken),false);
         return $accesstoken;
      }      
   }

   public function post_params($auth_code,$token){
      global $session;
      if($token == 'access_token'){
         $fields=array(
            'code'=>  $auth_code,
            'client_id'=>  $session->get_client_id(),
            'client_secret'=>  $session->get_client_secret(),
            'redirect_uri'=>  $session->get_redirect_uri(),
            'grant_type'=>  'authorization_code'
         );
      } else if($token == 'refresh_token'){
         $fields=array(
            'refresh_token'=>  $auth_code,
            'client_id'=>  $session->get_client_id(),
            'client_secret'=>  $session->get_client_secret(),
            'redirect_uri'=>  $session->get_redirect_uri(),
            'grant_type'=>  'authorization_code'
         );
      }
      $post = '';
      foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
      $post = rtrim($post,'&');   
      var_dump($post);
      return $post;
   }
3

1 Answer 1

0

I found out how to get the tokens, here is the code if anyone needs it Anyway oauth2 is not working with yahoo as i experienced, bellow i am using oauth1.0

    public function get_gmail_access_token($auth_code){
    global $session;
    $fields=array(
        'code'=>  $auth_code,
        'client_id'=>  $session->get_client_id(),
        'client_secret'=>  $session->get_client_secret(),
        'redirect_uri'=>  $session->get_redirect_uri(),
        'grant_type'=>  'authorization_code'
    );
    $post = '';
    foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
    $post = rtrim($post,'&');   

    $url = 'https://accounts.google.com/o/oauth2/token';
    $result = $this->curl_query_post($url,$post);
    $response =  json_decode($result);
    return $response->access_token; 
}   

public function get_yahoo_access_token($token,$verifier){
    global $session;
    $tokens = array();
    $fields=array(
        'oauth_version'=> '1.0',
        'oauth_nonce'=>  rand(),                
        'oauth_consumer_key'=>  $session->get_client_id(),
        'oauth_signature'=>  $session->get_client_secret().'%26'.$session->get_token_secret(),              
        'oauth_timestamp'=>  time()+3600,
        'oauth_signature_method'=>  'plaintext',
        'oauth_token'=>  $token,                        
        'oauth_verifier'=>  $verifier,          
        'oauth_callback'=>  $session->get_redirect_uri()                                
    );

    $post = '';
    foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
    $post = rtrim($post,'&');       

    $url = 'https://api.login.yahoo.com/oauth/v2/get_token';
    $result = $this->curl_query_post($url,$post);
    $string = parse_str($result);

    $tokens['oauth_token'] = $oauth_token;
    $tokens['oauth_token_secret'] = $oauth_token_secret;
    //$xoauth_yahoo_guid;       
    return $tokens;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.