OAuth2,令牌密钥不保持会话

时间:2016-06-23 07:55:15

标签: php oauth-2.0 yahoo-api gemini

所以我正在尝试让会话保持活跃状态​​,当我刷新会话到期时,给我这个:

  

收到错误:400 Raw   响应:{ “错误”: “SESSION_EXPIRED”, “ERROR_DESCRIPTION”:“会话   过期“}

这是我的代码基于雅虎的Gemini documentation给出的示例,我已经读完了,但没有什么可以阻止会话到期。

<?php
/* Example code to access Gemini API: Fetch advertiser information, create a new campaign and read specific campaign data

Prerequisites:
  1. Sign up for an account on https://admanager.yahoo.com
  2. Download YahooOAuth2.class.php file from here: https://github.com/saurabhsahni/php-yahoo-oauth2/blob/master/YahooOAuth2.class.php
  3. PHP modules for json_decode & curl
  4. A webserver running this code on port 80/443. Yahoo OAuth callback is only supported on these ports
*/
require "YahooOAuth2.class.php";
session_start();
#Your Yahoo API consumer key & secret with access to Gemini data

define("CONSUMER_KEY","<your consumer key>");
define("CONSUMER_SECRET","<your consumer secret>");
$redirect_uri="http://".$_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];//Or your other redirect URL - must match the callback domain

$gemini_api_endpoint="https://api.admanager.yahoo.com/v1/rest";

$oauth2client=new YahooOAuth2();

if (isset($_GET['code'])){
    $code=$_GET['code'];
    $_SESSION['code'] = $_GET['code'];
}
else {
    $code=0;
}

if($code){
     #oAuth 3-legged authorization is successful, fetch access token
     $_SESSION['token'] = $oauth2client->get_access_token(CONSUMER_KEY,CONSUMER_SECRET,$redirect_uri,$_SESSION['code']);

     #Access token is available. Do API calls.

     $headers = array('Authorization: Bearer '. $_SESSION['token'],'Accept: application/json','Content-Type: application/json');

     #Fetch Advertiser Name and Advertiser ID
     $url=$gemini_api_endpoint."/advertiser/";

     $resp=$oauth2client->fetch($url,$postdata="",$auth="",$headers);
     $jsonResponse = json_decode( $resp);
     $advertiserName = $jsonResponse->response[0]->advertiserName;
     $advertiserId = $jsonResponse->response[0]->id;
     echo "Welcome ".$advertiserName;
}
else {
    # no valid access token available, go to authorization server
    header("HTTP/1.1 302 Found");
    header("Location: " . $oauth2client->getAuthorizationURL(CONSUMER_KEY,$redirect_uri));
    exit;
}

?>

正如你所看到的,我已经尝试了session_start();,将$ _GET ['code']和$ _GET ['token']保存到会话中,但这不起作用。

我是对的,这是保存令牌的问题吗?我花了一天的时间在它上面,感觉我在四处走动。

1 个答案:

答案 0 :(得分:0)

以下代码适用于我。如果这有帮助,请告诉我。

对于第一个查询,请尝试以下方法:

.then

然后进行下一个查询(如果需要,可以通过循环重用):

pandas 0.12.0

来自YahooOAuth2.class.php文件:

require "YahooOAuth2.class.php"; 

#Your Yahoo API consumer key & secret with access to Gemini data 
define("CONSUMER_KEY","");
define("CONSUMER_SECRET","");
$redirect_uri="http://".$_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];//Or your other redirect URL - must match the callback domain 

$gemini_api_endpoint="https://api.admanager.yahoo.com/v1/rest";
$oauth2client=new YahooOAuth2();

if (isset($_GET['code'])){
$code=$_GET['code'];  
} 
else {
$code=0;
}

if($code){
#oAuth 3-legged authorization is successful, fetch access token  
$tokens=$oauth2client->get_access_token(CONSUMER_KEY,CONSUMER_SECRET,$redirect_uri,$code);

#Access token is available. Do API calls.   
$headers= array('Authorization: Bearer '.$tokens['a'],'Accept: application/json','Content-Type: application/json');
#Fetch Advertiser Name and Advertiser ID
$url=$gemini_api_endpoint."/reports/custom";
$resp=$oauth2client->fetch($url,$postdata=$json,$auth="",$headers);
$jsonResponse = json_decode( $resp );
}

else {
# no valid access token available, go to authorization server 
header("HTTP/1.1 302 Found");
header("Location: " . $oauth2client->getAuthorizationURL(CONSUMER_KEY,$redirect_uri));
exit;
}