2

I am using Yii and the google API for yii from here yii google api. I want to fetch the contact list with this api but this is throwing the error

stdClass Object ( [error] => invalid_grant [error_description] => Code was already redeemed. )

I am using like this

public function actionIndex()
    {
        $client = Yii::app()->GoogleApis->client;
        $client -> setAccessType('online');
        $client -> setScopes('https://www.google.com/m8/feeds');
        $client->authenticate();

        if (isset($_GET['code'])) {
            $auth_code = $_GET["code"];
            Yii::app()->session['auth_token']= $auth_code;
            $this->redirect(array('/addcontacts/searchsuggesions'));
        }
        $this->render('index');
    }

public function actionSearchsuggesions()
    {
        if(Yii::app()->session['auth_token'])
        {
            $auth_code = Yii::app()->session['auth_token'];
            $max_results = 200;
            $fields=array(
                'code'=>  urlencode($auth_code),
                'client_id'=>  urlencode('**********'),
                'client_secret'=>  urlencode('**********'),
                'redirect_uri'=>  urlencode('http://localhost/addcontacts'),
                'grant_type'=>  urlencode('authorization_code')
            );
            $post = '';
            foreach($fields as $key=>$value)
            {
                $post .= $key.'='.$value.'&';
            }
            $post = rtrim($post,'&');
            $result = $this->curl('https://accounts.google.com/o/oauth2/token',$post);
            $response =  json_decode($result);
            print_r($response);die;
        }
    }

i don't know where i am wrong . if someone have clue please tell me .

Manoj Dhiman
  • 5,096
  • 6
  • 29
  • 68
  • 1
    maybe this link helps you. This person made an effort to get solutions for issues common like yours. http://stackoverflow.com/questions/33754177/google-analytics-custom-plugin-getting-error-invalid-grant – izk Mar 04 '16 at 10:52
  • but no answer for it .:( – Manoj Dhiman Mar 04 '16 at 10:54
  • The Code was already redeemed error means that you are trying to use an authorization code that has already been used. A given authorization code can only be used once. Please make sure you are using the authorization code passed in the request to the oauth2_callback when you submit the OAuth request to get the refresh token. – RK12 Mar 04 '16 at 10:57
  • 1
    @baboizk thanks . you save my day . :) – Manoj Dhiman Mar 04 '16 at 13:28

1 Answers1

1

I have solved this . I just need to use function to get access token by function $client->getAccessToken().

public function actionIndex()
    {
        $client = Yii::app()->GoogleApis->client;
        $client -> setAccessType('online');
        $client -> setScopes('https://www.google.com/m8/feeds');
        $client->authenticate();

        if (isset($_GET['code'])) {

            $accesstoken=json_decode($client->getAccessToken());
            $accesstoken=$accesstoken->access_token;
            Yii::app()->session['accesstoken']=$accesstoken;
            $this->redirect(array('/'));
        }
        $this->render('index');
    }
Manoj Dhiman
  • 5,096
  • 6
  • 29
  • 68