将 OAuth 令牌传递给 Yahoo API


Passing an OAuth token to the Yahoo API

我使用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;        

获取 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);

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