我在这里yii google api使用Yii和谷歌API for yii。我想用这个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;
}
}
我不知道我错在哪里。如果有人有线索请告诉我。
答案 0 :(得分: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');
}