I'm trying to connect with the Reddit API and programatically submit posts. I have managed to return an access token but I get 403 error when trying to do the actual submission. Any advice?
Get Access Token: (works, returns token)
$ch = curl_init('https://www.reddit.com/api/v1/access_token?grant_type=client_credentials&username=USERNAME&password=PASSWORD');
curl_setopt($ch, CURLOPT_USERPWD, 'CLIENTID:CLIENTSECRET');
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($ch);
curl_close($ch);
$token = json_decode($return, true);
Submit Link: (error 403)
$ch = curl_init('https://www.reddit.com/api/v1/submit?kind=link&sr=news&title=TITLE&r=news&url='.urlencode('http://example.com'));
curl_setopt($ch, CURLOPT_USERPWD, 'CLIENTID:CLIENTSECRET');
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: bearer ".$token['access_token']));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($ch);
curl_close($ch);
return json_decode($return, true);
Related
I am posting to an API to get an authorization token. The problem is this token is only activated for 24 hours so I am creating a cronjob that gets a new token every 24 hours.
This is my post using CURL:
<?PHP
// GET auth token
$url = 'https://website.nl';
$data = array("client_id" => "myclientid","client_secret" => "mysecret");
$postdata = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
echo '<pre>';
print_r ($result);
echo '</pre>';
?>
This returns:
{
"payload": "{\"access_token\":\"my_auth_token\",\"expires_in\":86400,\"token_type\":\"Bearer\"}"
}
How can I get access_token as the only output? I've tried echoing payload but I am not sure how to do it.
echo $result[0]->payload;
echo $result->payload;
Both don't work.
You have forgotten to json_decode
$url = 'https://jsonplaceholder.typicode.com/todos/1';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json'));
$result = curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result));
Please take a look at playground
looks like you're getting a JSON response from the curl request.
With the dummy parameters in the code I could't test it, but I guess the solution would be something like this:
<?PHP
// GET auth token
$url = 'https://website.nl';
$data = array("client_id" => "myclientid","client_secret" => "mysecret");
$postdata = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode(stripslashes($response));
echo $result->payload->access_token;
?>
Let me know if it works.
First I send a POST request with a login and a password (admin authorization) and get some fields.
Next, I need to send GET request to API (check that client email exists), but I receive CURL error: The requested URL returned error: 401
In Postman GET request is working, what I am doing wrong?
This is my GET Request:
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_URL, "www.myurl.com/clients?email=a#a.a");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERPWD, "myusername:mypassword");
$result = curl_exec($ch);
You have to pass query string in case GET.
try below updated code.
$qry_str = "?email=a#a.a"
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_URL, 'www.myurl.com/clients.php',$qry_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERPWD, "myusername:mypassword");
$result = curl_exec($ch);
Changing the code to an access token gives this error:
Error: { "status": 403, "message": "Forbidden" }
This is my code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,"grant_type=authorization_code&client_id=4853355452345362719&client_secret=deb78310995ec1cf00918a5e688e2148e6043bd640ab16f0f7ecd7543b4ac764&code=".$code);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result =curl_exec ($ch);
curl_close($ch);
print_r($result);
First, you need to generate an access token. This can be done CURLing the following URL:
"https://api.pinterest.com/oauth?response_type=code&redirect_uri={$callBackUrl}&client_id={$clientId}&scope=read_public,write_public"
This will return an authorization code, you can then use this to generate your access token using this URL: https://api.pinterest.com/v1/oauth/token
with these parameters.
$url = "https://api.pinterest.com/v1/oauth/token";
$body = "grant_type=authorization_code&client_id={$clientId}&client_secret={$clientSecret}&code={$code}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$content = curl_exec($ch);
curl_close($ch);
$data = json_decode($content, true);
403 Forbidden usually suggests a permissions error. I'd bet the remote API does not recognize the code you're sending
I am getting {"int_err_code":"Authentication Failed","msg":"Wrong grant type"} trying to get access_token and organizer_key. I use GoToWebinar API.
Here is my curl:
$http_headers=array(
'Accept:application/json',
'Content-Type:application/x-www-form-urlencoded'
);
$URL = 'https://api.citrixonline.com/oauth/access_token?grant_type=authorization_code'
.'&code='.$response_key
.'&client_id='.$consumer_key;
// send curl post request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $http_headers);
$response = curl_exec($ch);
curl_close($ch);
Here is what I am currently trying:
if (function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/me/feed');
curl_setopt($ch, CURLOPT_POST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, "access_token=$facebook_access_token&message=testing api.");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
}
where $facebook_access_token is a variable containing the access token.
Am I doing something wrong? I have to use the php api and I do not explicitly know the users facebook page url, only the access token.
Thanks.
Add one more option to cURL:
CURLOPT_SSL_VERIFYPEER : false
if (function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/me/feed');
curl_setopt($ch, CURLOPT_POST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, "access_token=$facebook_access_token&message=testing api.");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
}