将OAuth令牌传递给Yahoo API

时间:2015-01-12 13:28:15

标签: php api yahoo

我使用Yahoo API创建了一个新的应用程序。如何使用CURL功能传递所需的标头?我尝试时收到此错误消息:

<yahoo:error xml:lang="en-US"><yahoo:description>Please provide valid credentials. OAuth oauth_problem="unable_to_determine_oauth_type", realm="yahooapis.com"</yahoo:description></yahoo:error>

如何在此代码中传递所需的标头:

$url ="http://fantasysports.yahooapis.com/fantasy/v2/team/223.l.431.t.1";  
$ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    
curl_setopt($ch, CURLOPT_URL, $url);   
//get the url contents  
$data = curl_exec($ch);     
//execute curl request curl_close($ch);   
$xml = simplexml_load_string($data);    
print_r($xml);    
exit;        

1 个答案:

答案 0 :(得分:1)

获得OAuth 2.0访问令牌后,请在以下代码中使用它:

$token = "<token>";

$url = "https://fantasysports.yahooapis.com/fantasy/v2/team/223.l.431.t.1?format=json";

$headers = array(
  'Authorization: Bearer ' . $token,
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
$json = json_decode($response);
print_r($json);

请注意,它使用Authorization URL方案通过安全传输通道在https HTTP标头中显示令牌,并请求使用format URL查询参数将内容作为JSON返回