【问题标题】:Code was already redeemed yii代码已兑换 yii
【发布时间】:2016-06-18 01:31:21
【问题描述】:

我在yii google api 使用 Yii 和 yii 的 google API。我想用这个 api 获取联系人列表,但这会引发错误

stdClass 对象 ( [error] => invalid_grant [error_description] => 代码 已经被赎回。 )

我是这样用的

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

我不知道我错在哪里。如果有人有线索请告诉我。

【问题讨论】:

  • 也许这个链接对你有帮助。此人努力为像您这样的常见问题寻求解决方案。 stackoverflow.com/questions/33754177/…
  • 但没有答案。:(
  • 代码已兑换错误表示您正在尝试使用已使用的授权代码。给定的授权码只能使用一次。请确保您在提交 OAuth 请求以获取刷新令牌时使用在请求中传递给 oauth2_callback 的授权代码。
  • @baboizk 谢谢。你救了我的命。 :)

标签: php yii google-api-php-client


【解决方案1】:

我已经解决了这个问题。我只需要使用函数通过函数$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');
    }

【讨论】:

  • 做得很好。我只提供了一个链接,所以没有做太多。很高兴你解决了它!
  • 但这帮助我发现无需再次获取访问令牌。再次感谢:)
猜你喜欢
  • 2017-12-08
  • 1970-01-01
  • 1970-01-01
  • 2015-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多