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;
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.
Related
While using connect with paypal on sandbox credential during token api call i always get the err
Array ( [error] => invalid_client [error_description] => Client Authentication failed )
Below shown is the CURL call i am using. please help me out
$code = $_GET['code'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.sandbox.paypal.com/v1/oauth2/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=authorization_code&code=".$code."");
$headers = array();
$headers[] = 'Authorization: Basic client_id:client_secret';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$output = json_decode($result,true);
print_r($output);
As a comment mentions, CURLOPT_USERPWD is the simplest strategy, let curl do the work for you
If you were to be setting the Authorization header yourself, client_id:client_secret would need to be Base64 encoded
As a side note, when using curl on the command line the equivalent is the -u flag
```` echo "<br/><br/>Generating Form Digest<br/>";
// Initialize curl for Getting the Form Digest
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, "https://xxxxxxxxxxxxxx.sharepoint.com/_api/contextinfo");
// Set curl Method
curl_setopt($ch, CURLOPT_POST, true);
// Set HTTP Header for POST request
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Length: 0',
'Accept : application/json;odata=verbose',
// 'Authorization: Bearer ' . $access_token,
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $url_client);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
echo "<br/> Result of API Context :<br/>";
print_r(json_decode($result));
curl_close($ch);
echo "<br/><br/><br/>Uploading File<br/>";
// API URL with the file attached
$api_URL =
"https://xxxxxxxxxxxxxxxxxxxxxxxxxx.sharepoint.com/_api/web/GetFolderByServerRelativeUrl
('Documents')/Files/add(url='uploads/{$_FILES["fileToUpload"]["name"]}',overwrite=true)";
echo $api_URL;
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $api_URL );
curl_setopt($ch, CURLOPT_POST, true);
// Set HTTP Header for POST request
// Set the headers in the curl object
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
// 'Content-Type: application/x-www-form-urlencoded',
"Authorization: Bearer {$json_result->access_token}",
"Content-Length: {$filesize}",
"X-HTTP-Method: MERGE",
//"X-RequestDigest:",
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
echo "<br/> RESULT OF Upload file CURL<br/> " ;
// Convert result to JSon
$json_result = json_decode($result);
// Print Json Result
print_r($json_result);
// Close CURL
curl_close($ch);````
What I'm trying to do is to create a PHP webpage that will take a file ans an iput and upload it directly yo a shrepoint library
I'm generating access token before this code is executed
I'm getting the "404 unauthorized" error when form digest is generated, please help !!
I am trying to use googles API, which you can only used when authenticated with oAuth.
I have successfully obtained the access key, however I can't seem to be able to use it with the API. This is my code:
$accessToken = $client->fetchAccessTokenWithAuthCode($_GET["code"])["access_token"];
$ch = curl_init();
$url = "https://www.googleapis.com/androidpublisher/v2/applications/flarehubpe.flarehub.xflare/purchases/products/flarehubvip/tokens/fraud_token";
$headers = array("Authorization: Bearer $accessToken");
curl_setopt($ch, CURLOPT_URL, $url); # URL to post to
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); # return into avariable
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); # custom headers, see above
$result = curl_exec($ch); # run!
curl_close($ch);
var_dump("Result", $result);
//Outputs: "result" nothing else.
What am I doing wrong?
I have built a prototype calendar synching system using the Google calendar API and it works well, except refreshing access tokens. These are the steps I have gone through:
1) Authorised my API and received an authorisation code.
2) Exchanged the authorisation code for Access Token and a RefreshToken.
3) Used the Calendar API until the Access Token expires.
At this point I try to use the Refresh Token to gain another Access Token, so my users don't have to keep granting access because the diary sync happens when they are offline.
Here's the PHP code, I'm using curl requests throughout the system.
$requestURL = "https://accounts.google.com/o/oauth2/token";
$postData = array("grant_type" => "refresh_token",
"client_id" => $clientID,
"client_secret" => $clientSecret,
"refresh_token" => $refreshToken);
$headers[0] = 'Content-Type: application/json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $requestURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
$response = curl_exec($ch);
$responseArray = json_decode($response, TRUE);
The response i'm getting is:
[error] => invalid_request
[error_description] => Required parameter is missing: grant_type
No curl errors are reported.
I've tried header content-type: application/x-www-form-urlencoded, and many other things, with the same result.
I suspect it's something obvious in my curl settings or headers as every parameter mentioned in the Google documentation for this request is set. However, I'm going around in circles so would appreciate any help, including pointing out any obvious errors I've overlooked.
your request should not post JSON data but rather query form encoded data, as in:
$requestURL = "https://accounts.google.com/o/oauth2/token";
$postData = "grant_type=refresh_token&client_id=$clientID&client_secret=$clientSecret&refresh_token=$refreshToken";
$headers[0] = 'Content-Type: application/x-www-form-urlencoded';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $requestURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($ch);
$responseArray = json_decode($response, TRUE);
I saw this post on consuming a web service using CURL: Consume WebService with php
and I was trying to follow it, but haven't had luck. I uploaded a photo of the web service I'm trying to access. How would I formulate my request given the example below, assuming the URL was:
https://site.com/Spark/SparkService.asmx?op=InsertConsumer
I attempted this, but it just returns a blank page:
$url = 'https://xxx.com/Spark/SparkService.asmx?op=InsertConsumer?NameFirst=Joe&NameLast=Schmoe&PostalCode=55555&EmailAddress=joe#schmoe.com&SurveyQuestionId=76&SurveyQuestionResponseId=1139';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
$xmlobj = simplexml_load_string($result);
print_r($xmlobj);
Really, you should probably look at the SOAP extension. If it is not available or for some reason you must use cURL, here is a basic framework:
<?php
// The URL to POST to
$url = "http://www.mysoapservice.com/";
// The value for the SOAPAction: header
$action = "My.Soap.Action";
// Get the SOAP data into a string, I am using HEREDOC syntax
// but how you do this is irrelevant, the point is just get the
// body of the request into a string
$mySOAP = <<<EOD
<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope>
<!-- SOAP goes here, irrelevant so wont bother writing it out -->
</soap:Envelope>
EOD;
// The HTTP headers for the request (based on image above)
$headers = array(
'Content-Type: text/xml; charset=utf-8',
'Content-Length: '.strlen($mySOAP),
'SOAPAction: '.$action
);
// Build the cURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $mySOAP);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// Send the request and check the response
if (($result = curl_exec($ch)) === FALSE) {
die('cURL error: '.curl_error($ch)."<br />\n");
} else {
echo "Success!<br />\n";
}
curl_close($ch);
// Handle the response from a successful request
$xmlobj = simplexml_load_string($result);
var_dump($xmlobj);
?>
The service requires you to do a POST, and you're doing a GET (curl's default for HTTP urls) instead. Add this:
curl_setopt($ch, CURLOPT_POST);
and add some error handling:
$result = curl_exec($ch);
if ($result === false) {
die(curl_error($ch));
}
This is the best answer because using this once you need to login then
get some data from webservices(third party site data).
$tmp_fname = tempnam("/tmp", "COOKIE"); //create temporary cookie file
$post = array(
'username=abc#gmail.com',
'password=123456'
);
$post = implode('&', $post);
//login with username and password
$curl_handle = curl_init ("http://www.example.com/login");
//create cookie session
curl_setopt ($curl_handle, CURLOPT_COOKIEJAR, $tmp_fname);
curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $post);
$output = curl_exec ($curl_handle);
//Get events data after login
$curl_handle = curl_init ("http://www.example.com/events");
curl_setopt ($curl_handle, CURLOPT_COOKIEFILE, $tmp_fname);
curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($curl_handle);
//Convert json format to array
$data = json_decode($output);
echo "Output : <br> <pre>";
print_r($data);
echo "</pre>";