I'm trying to use the Pinterest API using PHP, but so far it doesn't really work.
I use this guide to get started:
https://developers.pinterest.com/docs/api/authentication/
I got everything working now except for step 3.
The documentation says it's just a simple request to
https://api.pinterest.com/v1/me/?access_token=<YOUR-ACCESS-TOKEN>
Making authenticated requests
Finally, once you have an access token, you can make authorized
requests on the users behalf using OAuth 2.0 Bearer tokens supplied
via the Authorization request header or via a request parameter named
access_token (supplied as either a form-encoded body parameter or a
URI query parameter). The Authorization header is preferred.
For example, you can visit this site on your browser:
https://api.pinterest.com/v1/me/?access_token= A
sample response might look like:
{
"data": {
"url": "https://www.pinterest.com/ben/",
"first_name": "Ben",
"last_name": "Silbermann",
"id": "4788400174839062"
} }
I have an access_token which looks like this:
AcFFayZKwqXuKVkj1J-0QN1fCob1FAgjuP9-OtFCg_j49UAgogXXXXX
But everytime I get this message:
{"status": "failure", "code": 3, "host": "coreapp-devplatform-devapi-181", "generated_at": "Mon, 28 Sep 2015 12:28:38 +0000", "message": "Authorization failed.", "data": null}
I use this PHP code to get data from the URL:
$curl = curl_init($url);
curl_setopt_array($curl, array(
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_FORBID_REUSE => true,
CURLOPT_FRESH_CONNECT => true,
CURLOPT_HEADER => false,
CURLOPT_HTTPHEADER => array("Content-Type: application/x-www-form-urlencoded"),
CURLOPT_NOBODY => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_TIMEOUT => 30,
CURLOPT_USERAGENT => self::userAgent()
));
$result = curl_exec($curl);
var_dump($result);
$errorCode = curl_errno($curl);
$respCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
I just gone through this same error, but for different endpoint. Try this code
$ch = curl_init();
$url = "https://api.pinterest.com/v1/me/?access_token='YOUR_ACCESS_TOKEN'";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = trim(curl_exec($ch));
curl_close($ch);
print_r($result);
Please have a look at this discussion. It covers the exact same issue you are describing. As far as I can tell right now it seems to be an issue on Pinterest's side. #ZackArgyle from Pinterest's dev team got notified and is working on it I believe.
Related
I got a problem when getting access_token from OAuth2 Server. I'm using curl in PHP to get the access_token, but it failed and return:
error: "invalid_client",
error_description: "Client authentication failed."
The strange is, if I use the curl from command prompt:
curl -d "client_id=f3d259ddd3ed8ff3843839b&client_secret=4c7f6f8fa93d59c45502c0ae8c4a95b&redirect_uri=http://application.dev/oauth/handle&grant_type=authorization_code&code=rZCQQBXVSQqKi2IRro1gYkSsRhyUcLsNODACjwPw" http://oauth-server.dev/oauth/access_token
it success and return the access_token:
"access_token":"fI7APDRZygrsF1BiegAQCS1yUT8vnm1LgD5bIu2U",
"token_type":"Bearer",
"expires_in":3600
I'm using Laravel 5.2 and here's my code to handle from OAuth Server to get access_token:
public function getOAuthHandle(Request $request){
$url = 'http://oauth-server.dev/oauth/access_token';
$code = $request->code;
$client_id = 'f3d259ddd3ed8ff3843839b';
$client_secret = '4c7f6f8fa93d59c45502c0ae8c4a95b';
$redirect_uri = 'http://application.dev/oauth/handle';
$clienttoken_post = array(
"code" => $code,
"client_id" => $client_id,
"client_secret" => $client_secret,
"redirect_uri" => $redirect_uri,
"grant_type" => "authorization_code"
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$json_response = curl_exec($curl);
curl_close($curl);
return $json_response;
}
I have tried to another script from internet and stackoverflow and restart Apache and PHP, but it still doesn't work.
Is there any way to solve this problem? What should I check?
Thank you for your help and answer.
Try to send the request on curl headers instead.
$curl = \curl_init();
\curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 600,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_HTTPHEADER => $clienttoken_post,
CURLINFO_HEADER_OUT => true,
));
$response = \curl_exec($curl);
Either sent the grant_type on headers and then client_secret and client_id as post, or sent them on headers try it out. And btw. $request->code should be checked before passing to curl request. And all the url, client_secret, client_id MUST be with in .env and get them through config/services.
I'm creating a small web app allowing people to update their video outside of YouTube. I'm currently testing for myself and i'm running into the
{ "error": { "errors": [ { "domain": "youtube.video", "reason": "videoNotFound", "message": "The video that you are trying to update cannot be found. Check the value of the \u003ccode\u003eid\u003c/code\u003e field in the request body to ensure that it is correct.", "locationType": "other", "location": "body.id" } ], "code": 404, "message": "The video that you are trying to update cannot be found. Check the value of the \u003ccode\u003eid\u003c/code\u003e field in the request body to ensure that it is correct." } }
Things that I know for certain:
I have the correct authorization code
I have the correct channel this authorization code refers to.
I have the correct video id.
I'm sending a PUT request using cURL in PHP as follows:
$curl = curl_init($url . "https://www.googleapis.com/youtube/v3/videos?part=snippet&access_token=".$token)
$data = array(
'kind' => 'youtube#video',
'id' => 'theCorrectIdIsHere',
'snippet' => array(
"title" => "Test title done through php",
"categoryId" => "1"
),
);`
So how come when I execute this using:
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:
application/json'));
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));`
A similar question has been asked: Update title and description using YouTube v3 API?, however, the answer involved him downloading and replacing the video which costs extra quota and simply shouldn't have to be done
Note Everything works fine on the API test done here: https://developers.google.com/youtube/v3/docs/videos/update
Use json_encode to set the request data & using Authorization header to set access token :
<?php
$curl = curl_init();
$access_token = "YOUR_ACCESS_TOKEN";
$data = array(
'kind' => 'youtube#video',
'id' => 'YOUR_VIDEO_ID',
'snippet' => array(
"title" => "Test title done through php",
"categoryId" => "1"
)
);
curl_setopt($curl,CURLOPT_URL, "https://www.googleapis.com/youtube/v3/videos?part=snippet");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: Bearer ' . $access_token));
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_VERBOSE, true);
$result = curl_exec($curl);
curl_close($curl);
var_dump($result);
?>
I read some Q&As but still struggling with this one. I need to post a specific array to an API and get another array as an answer.
UPDATE
I used :
<?php
echo 'Testing cURL<br>';
// Get cURL resource
$curl = curl_init();
$data=array(array("UserId"=>"xxxx-10100","Password"=>"pass"));
$sendpostdata = json_encode( array( "postdata"=> $data ) );
echo $sendpostdata;
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://cloud.servicebridge.com/api/v1/Login',
CURLOPT_HTTPHEADER => array('Accept: application/json','Content-Type: application/json'),
CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $sendpostdata
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
echo $resp;
// Close request to clear up some resources
curl_close($curl);
?>
This results in
Testing cURL
{"postdata":[{"UserId":"xxxxx-10100","Password":"pass"}]}
{ "Data": null, "Success": false, "Error": { "Message": "Invalid UserId: ", "Value": "InvalidUserId", "Code": 9001 } }
No console logs, no other messages or clues
I am trying to implement this API https://cloud.servicebridge.com/developer/index#/
to Worpdress.
The API requires a
{
"UserId": "string",
"Password": "string"
}
Can you help me out? What am I doing wrong
Really appreciate this,
Giannis
I have checked the given API and it's REQUEST Parameters to access them and I think your initial data (username and password) is not going correctly. Please use this code:-
<?php
error_reporting(E_ALL); // check all type of errors
ini_set('display_errors',1); // display those errors
// Get cURL resource
$curl = curl_init();
$sendpostdata = json_encode(array("UserId"=>"xxxx-10100","Password"=>"pass"));
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://cloud.servicebridge.com/api/v1/Login',
CURLOPT_HTTPHEADER => array('Accept: application/json','Content-Type: application/json'),
CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $sendpostdata
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
echo $resp;
// Close request to clear up some resources
curl_close($curl);
?>
Note:- change UserId & Passwordvalues to your real values.Thanks
This might help you in generating proper json. Hope this will work.
use this
$data=array(array("UserId"=>"xxxxx-10100","Password"=>"pass"));
instead of
$data ='[{"UserId":"xxxxx-10100","Password": "pass"}]';
hello i'm using code used by other people who supposedly have gotten it to work and have gotten their token information retrieved. The code is as follows:
$ch = curl_init();
$clientId = "myclientid";
$secret = "mysecret";
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSLVERSION , 6); //NEW ADDITION
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
$result = curl_exec($ch);
if(empty($result))die("Error: No response.");
else
{
$json = json_decode($result);
print_r($json->access_token);
}
curl_close($ch); //THIS CODE IS NOW WORKING!
I retrieved this code from Paypal API with PHP and cURL and have seen it implemented in some others peoples code so i assume that it works. However i'm only receiving no response even though i am supplying the correct client id and secret (maybe a recent update has broken this code?).
The guide provided by Paypal on getting the access token is found here-> https://developer.paypal.com/docs/integration/direct/make-your-first-call/
however it demonstrates the solution in cURL and not through the PHP cURL extension so its alittle cryptic to me. Any help?
Well it seems that it is now a requirement to declare what type of SSL Version to use, thus the code above will work when curl_setopt($ch, CURLOPT_SSLVERSION , 6); //tlsv1.2 is inserted.
Just tested the API and I can confirm this code is working :
(No SSL options)
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sandbox.paypal.com/v1/oauth2/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_USERPWD => $PAYPAL_CLIENT_ID.":".$PAYPAL_SECRET,
CURLOPT_POSTFIELDS => "grant_type=client_credentials",
CURLOPT_HTTPHEADER => array(
"Accept: application/json",
"Accept-Language: en_US"
),
));
$result= curl_exec($curl);
$array=json_decode($result, true);
$token=$array['access_token'];
echo "<pre>";
print_r($array);
I am using cURL to call a REST API. And the below code works for GET method , but however for POST method call it doesn't work after i add :
curl_setopt($ch, CURLOPT_POST, true);
What could be the cause ?
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false, // Disabled SSL Cert checks
CURLOPT_HTTPHEADER => array('appId: C579929D-F0AA-4A8F-B6C6-1FF96694483C','appKey: AE78E7F1-FBDE-45F5-BAC2-210CEE9D3ED9')
);
$url = "some-url";
$ch = curl_init($url);
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_POST, true);
$output = curl_exec($ch);
echo $output;
P.S
I double checked the API call using PostMan and it returns the expected result.
Do i need to add additional properties to API call ?
And i have to call REST API of format :
https://api.abc.com/v1/transactions/companyid/123/userid/456/description/ppeck/source/PPA
And as suggested in the answer i have updated my code as :
$url = "https://api.abc.com/v1/transactions";
$ch = curl_init($url);
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "companyid=12301&userid=31401&description=ppa-check&source=PPA&amount=123&txndate=010111&txntypeid=420&customerid=2701");
$output = curl_exec($ch);
And sadly it didn't worked.
try
post fields must be as a query string with appending (&) like
$url = 'http://example.com'; //your api call url
$fields = 'q=example&y=fgt&ghty'; // all posting fields separated with &
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);