Facebook error 2500 with valid access token - php

Previously I have been able to access the Facebook OAuth services through cURL in PHP. However, I am now getting a 2500 error (In this case An active access token must be used to query information about the current user.). I can access the information by using the URL straight through my web browser, however when I try to access it with cURL in PHP it doesnt work. I am using Facebook API v1.0, and the token is being generated with the Graph API Explorer.
What I have tried
I have tried creating a new access token, as well as tried using Facebook API v2.0 and the unversioned API. As I said above, I have tried using the exact same URLs in my browser, which works. I have also tried clearing cookies and trying again, as well as trying a whole different browser.
I have seen some of the other questions on SO which relate to this question but none of the answers work for me.
The code
The URL I am using is simply a call to the /me/home edge.
https://graph.facebook.com/v1.0/me/home/?since=1402913078&until=1402913213&access_token=<access token here>
Where <access token here> is the access token
The PHP cURL code looks like the following:
$url = "<the url here>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
// get the response here...

It turns out that the actual URL being requested by cURL was different to the one that should have been being requested (the GET parameters were missing). This was caused by another location in the code.
To verify that the URL that is being requested is the same as the one passed to cURL, use the following code:
$requested_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

Related

POST works in facebook Graph Explorer but not from code

I have a php script that attempts to POST a post to a Facebook page via CURL, but fails with the error.
(#200) If posting to a group, requires app being installed in the group, and either publish_to_groups permission with user token, or both pages_read_engagement and pages_manage_posts permission with page token; If posting to a page, requires both pages_read_engagement and pages_manage_posts as an admin with sufficient administrative permission
The exact same POST works from Facebooks API Graph Explorer. The same page access token is used in both cases. I have checked the token in Facebook's token debugger and the permissions mentioned in the error are both present.
The code I'm using is:
$post_content = "message=".$message."&access_token=".$ae_page_access_token;
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/v13.0/{page-ID}/feed');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_content);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
Interestingly, I can post photos with captions to the page with the same access token from my php script without an error. The error only happens when using https://graph.facebook.com/v13.0/{page-ID}/feed -- the permissions just don't seem to work in this case. No idea why.
Can anyone suggest how to troubleshoot further or a possible cause? Any advice gratefully received
Well this is embarrassing. Misspelled parameter in a function call was the culprit. Works now.
Try this
$post_content = "message=".$message;
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/v13.0/{page-ID}/feed?access_token=' . $ae_page_access_token);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_content);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
token place is in url like a get parameter https://developers.facebook.com/docs/facebook-login/guides/access-tokens#pagetokens

401 Unauthorized when authenticating using referer

I am trying to use an API which says to use it in a custom application to set a custom referer. Here's the official text:
The API requires (if enabled) a token for read operations to be send
to allow request, otherwise "Missing 'key' parameter" message will be
returned.
The security module will use the referrer heading ("referer" in Java,
HTTP_REFERER in PHP) of the request to identify and filter the
authorized domains.
I can currently log in to the web interface by using a special URL provided which seems to set the session and then redirects to the main URL. If I try navigating to the main URL directly without first using the special URL, I get a 401 error, as expected (afterwards, I can just use the main URL)
The key given below the text above is something like: http://example.com/api/?key=secretkeyhere
While I can use the API using the GUI tool in my web browser as described above, I can't figure out how to connect to it programatically. I have tried using both curl in Linux as well as curl in PHP. Both have failed.
curl from terminal:
curl --header http://example.com/api/?key=secretkeyhere http://example.com/api/search?query=terms*
curl in PHP:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com/api/search?query=terms');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://example.com/api/?key=secretkeyhere');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
$html = curl_exec($ch);
curl_close($ch);
echo "$html";
?>
I continue to get:
Unauthorized This server could not verify that you are authorized to
access the document requested. Either you supplied the wrong
credentials (e.g., bad password), or your browser doesn't understand
how to supply the credentials required.
Am I doing something wrong here? Is the API not set up correctly? I've read the man page for cURL and it seems like this is exactly what I should be doing.

cURL returns No project could be found error

I'm trying to GET a result via jira REST API using php, but I'm getting unexpected results. When I punch the following URL: http://localhost:8080/rest/api/2/project/ABCD/components directly on the browser I get a result(it works), but when I do it via php I get the following error:
string(76) "{"errorMessages":["No project could be found with key 'RELM'."],"errors":{}}"
the following is the php code:
$key = trim('RELM');
$ch = curl_init();
$url = "http://localhost:8080/rest/api/2/project/$key/components";
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
// curl_setopt($ch,CURLOPT_HEADER, false);
$output=curl_exec($ch);
curl_close($ch);
var_dump($output) ;
When you try that url from a browser you've probably logged in in JIRA first, but you don't have any authentication in your php code.
You need to be authenticated to get the correct results. E.g. you can use basic authentication with the credentials of a JIRA user that has permission to browse (or administer, depending on what you want to do) that project.
See also this question.

cURL request getting wrong request version in Google App Engine

I have a cURL request in my code which works fine when running locally:
$url = "http://ipinfo.io/{$_SERVER['REMOTE_ADDR']}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
$locale = json_decode($response);
and returns a JSON as expected. Our production system is on Google App Engine, however, where I get the website version for a browser rather than the JSON.
I can get this cURL request to work if I change
google_app_engine.enable_curl_lite = "1"
in the php.ini in the root directory of my project to
extension = "curl.so"
but Google's documentation insists the former is to be used on production. Additionally, using the latter breaks things like Monolog's SlackHandler.
Is there a way to get the JSON from this cURL request while still using Google's "cURL Lite"?
From the ipinfo.io documentation:
"We do a little bit of magic on the server to determine if we should send the JSON response or the webpage. We usually get it right, but if you're seeing the webpage instead of the JSON (or want to check the JSON output in a browser) you can force the JSON response by adding /json to the end of the URL"
Adding /json to the end of the URL worked for me in this case, but I wanted a more general solution. Since Google's cURL Lite uses their URL Fetch in the background, ipinfo.io's "magic" is somehow getting confused. I found that by specifying the Accept header then the /json addition wasn't required. In PHP:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
Thanks to the folks on the #php channel of NashDev Slack for helping me on this one!

Skydrive: Listing root files and folders in PHP

We would like to integrate Skydrive in our website, like we're currently doing with Google Drive but I'm lost in how to retrieve the root folders and files.
I've read these posts and docs:
Access SkyDrive using PHP and OAuth
Convert to PHP REST CURL POST
Skydrive API here: http://msdn.microsoft.com/en-us/library/live/
Curl doc on http://php.net - even if I'm still confused about that
And yet, I can't seem to get what I wanted.
I've already the access_token and it's correctly assigned, the scopes are also correct (I'm using wl.contacts_skydrive), so, I'm putting here part of the code I've so far so you can tell me what I'm missing, what I should add or if I'm taking a wrong turn.
//$token is correctly assigned
$url = 'GET https://apis.live.net/v5.0/me/skydrive/files?access_token='.$token;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
Thanks you for your time taken to help me.
You need to use CURLOPT_RETURNTRANSFER to return the transfer as a string.
Otherwise you will get true or false. Assuming you were getting true from curl_exec
Try adding
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

Categories