xyz
xyz

Reputation: 45

Passing an OAuth token to the Yahoo API

I have created a new app using the Yahoo API. How can I pass the required headers using CURL functionality? I got this error message when I tried:

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

How can I pass the required headers in this code:

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

Upvotes: 1

Views: 1980

Answers (1)

Hans Z.
Hans Z.

Reputation: 53928

Once you'd obtained an OAuth 2.0 access token, use it in the following code:

$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);

Notice that it presents the token in an Authorization HTTP header over a secure transport channel using the https URL scheme and requests to return the content as JSON using the format URL query parameter.

Upvotes: 1

Related Questions