This API call requires a valid app_id - facebook

I got something wierd going on. I have 2 Facebook API calls (using the Facebook PHP-SDK) next to each other. The first call goes fine, but the second throws an exception:
"This API call requires a valid app_id"
Previous to the API Call I requested an Access Token with offline_access and publish_stream. The token is valid because API calls next to this "problem call" will do fine. to make things clear, a small snippet of my code:
// this call goes fine!
$user_info = $this->facebook->api('/'.$user_id );
// create the wallpost for Facebook
$wallpost = array(
'message' => 'some message',
'name' => 'Shoppe',
'caption' => "Ik heb een product toegevoegd",
'link' => site_url(),
'description' => $description,
'picture' => site_url("assets/img/logo.png")
);
// this throws an exception "This API call requires a valid app_id"
$result = $this->facebook->api('/'.$user_id.'/feed/','post',json_encode($wallpost) );
$user_id is valid value, so is all the info in the $wallpost

I don't know PHP so the details of string concat are lost on me, but the first thing that popped into my head reading this is that your request string on the POST may not be formatted like you think it is so PHP gets a malformed request, cannot find the app ID and throws you an error.
I would suggest taking the string you are creating for the api() call and printing it out before making the call and making sure it looks exactly like it is suppose to look before going over the wire.

Related

Authorizing to Spotify API from Perl

I'd like to ask for clue how to fill-in correctly parameters, to get an access token to Spotify API. Using the LWP::Authen::OAuth2 module initialized following the guidelines at Spotify authorization guide
use LWP::Authen::OAuth2;
my $oauth2 = LWP::Authen::OAuth2->new(
client_id => '...',
client_secret => '...',
#authorization_endpoint => 'https://accounts.spotify.com/api/token',
#token_endpoint => 'https://accounts.spotify.com/api/token',
);
$oauth2->post(
'https://accounts.spotify.com/api/token',
grant_type => 'client_credentials'
);
The post method however gives an error:
"request" on an undefined value at D:\Perl\site\lib/LWP/Authen/OAuth2.pm line 104."
Furthermore, once I obtain the access token, can I feed it to standard LWP::UserAgent object or am I obligated to continue requests using the $oauth2 object? Thanks.

Facebook API 2.2: Graph error when trying to post a link

The goal of the script is to post automatically, on my profile and on my groups, the link to a specific page, once the content is created.
But all what I got when I launch the test script here below, is this returned string:
Graph returned an error: An active access token must be used to query information about the current user.
In fact, the token related to the app was given by FB Developers site.
I tried also the combination:
634060413388093|9ed702cc524a1cbb59ca1fb7a17839f1
But I still get the same return
<?php
$path = getcwd();
require_once $path . '/include/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '634060413388093',
'app_secret' => '9ed702cc524a1cbb59ca1fb7a17839f1',
'default_graph_version' => 'v2.2', ]);
$linkData = [
'link' => 'http://www.example.com/page',
'message' => 'here the new page on my site.',
];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/me/feed', $linkData, '634060413388093|bnTyPyRtsZSLHoc1B1w772cz3BU');
} catch (Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch (Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
echo 'Posted with id: ' . $graphNode['id'];
?>
You need to use a user access token to read the feed of a user. 'me' refers to the user that is associated with the user access token. An app access token is not associated with any specific user.
This leads to the second point, which is much more important. You should never reveal your app secret, especially not on Stack Overflow or anywhere public. You should hide that part of your SO Question, or better yet, create a new FB application.
Ok I found the way in this moment. I tried any example andy help, any suggestion but it didn't work out.
Thus, I post it hoping it helps somebody else with my same issue:
The access token, is not app_id|app_secret but it's obtained in another way:
To get the correct access otken, one should go to:
https://developers.facebook.com
under "Tools & Support" select "Graph API Explorer"
On the upper right corner, there is a drop down menu called "Application".
Click and select the created application.
Select "Access Token" shown, and copy/paste it in the field aside $linkDAta variable:
$response = $fb->post('/me/feed', $linkData, '{access-token}');
At that point, launching the script as it is, it worked out :)
EDIT:
If you need to post in groups, just change the '/me/feed' with '{group_id}/feed'

Facebook API photo upload from URL not working

Trying to upload photos by access token, album_id or user_id and URL but getting an error:
"(#324) Requires upload file"
"OAuthException"
I've checked the token and it's valid. Also upload from file works, but upload from URL does not. Facebook does not seem to want to recognise the "url" parameter. This is strange because I swear the other day when I tested this it worked fine.
Is anyone else having this problem?
My code:
$graph_url = "https://graph.facebook.com/ALBUM_ID/photos?access_token=TOKEN";
$postdata = http_build_query( array('message' => 'hello', 'url' => IMAGE_URL);
$opts = array('http' => array(
'method' => 'POST',
'header' =>
'Content-type: application/x-www-form-urlencoded',
'content' => $postdata));
$context = stream_context_create($opts);
$result = json_decode(file_get_contents($graph_url, false, $context));
I've also checked, it works in FB's graph API explorer, but does not work when using any REST client tool either.
UPDATE:
I got it to work by switching the order of the array inside http_build_query so that the URL is first (or removing the 'message' parameter). The second element is always ingored.
Can anyone see what I'm doing wrong? Why is the second parameter ignored in this case? While the upload "works" I'd still like the 'message' parameter to work so that the image description can also be loaded.
When you’re uploading an image via URL, you must not use the Content-type application/x-www-form-urlencoded – that’s for real file uploads done from your server, using the source parameter, only.

Facebook create an album graph api

I have written a facebook app which checks if an album exists, if it does not it creates one.
This was all working 2 weeks ago and now I go and check it to show the client and it is no longer working. I was thinking it could have something to do with Facebook and maybe they now require an SSL? I can't find any evidence of this on there website however.
The error I am getting is:
Warning: file_get_contents(https://graph.facebook.com/517578134925015/albums?access_token=AAAD5tUCp808BAIZBPk4eV11mlf9C92velLsDeZAm5mXhKZCkwpM3LNy7ax6BBmhuH4BVZBUN6Iycyt55NoXZAFSts9zHeCFNzT6FLJYucgT2SQG8fOYIP) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 500 Internal Server Error in /var/www/vhosts/hidden/development/cms/backend/publishAlbum.php on line 59
My current code is:
//If no album ID is set, create a new album.
$graph_url = "https://graph.facebook.com/".$pageId."/albums?" . "access_token=". $pageAccessToken;
$postdata = http_build_query(
array(
'name' => $albumName
)
);
$opts = array('http' =>
array(
'method'=> 'POST',
'header'=>
'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = json_decode(file_get_contents($graph_url, false, $context));
Any help is appreciated.
Thanks
EDIT:
The code I tried that uses the API.
$params = array(
'name' => 'testtest',
'message' => 'test test'
);
try{
$albumtResult = $facebook->api('/'.$pageId.'/albums', 'post', $params);
}
catch(FacebookApiException $e){
error_log($e);
$albumtResult = null;
}
$albumId = $albumtResult->id;
Here is the caught exception: http://pastebin.com/pxeyRTDn
Well, the reason for this error would be much easier to figure out, if you’d used the PHP SDK to make API calls, instead of doing them “by hand“ using file_get_contents … because using the SDK, you get an exception that should point to the error cause, whereas file_get_contents (by default) just throws a warning when it gets anything else than a 2xx HTTP status code for an answer, and does not let you access the response body (which will most likely contain a human-readable error message as well).
My advice: Use the SDK!
Otherwise, add to the context parameter that you want to see the returned data even if the status code signals an error. (details -> see manual)
After a lot of searching, someone created a bug report on Facebook that was almost identical to my problem.
Since being created it has had a lot of repo's and other people reporting that they are also having the same issue.
The priority on facebook has been changed to high so hopefully it gets fixed soon.
Facebook bug report: https://developers.facebook.com/bugs/376442565760536?browse=search_50361119879b15494037772
Thanks for everyones time.

integrating with Facebook like

I'd like to integrate the "like" button into my site, but I cannot make sense of the information available on the web. I read this article, which was in another stackoverflow article, but have some questions: http://www.saschakimmel.com/2010/05/how-to-capture-clicks-on-the-facebook-like-button/. I've also posted this question before and the answers I received really had nothing to do with what I was asking. My original question was asked here: Facebook Integration into website.
I've setup a "Page" already in facebook, and from what I understand in the link above, you need to setup an "App" to get an API key. What I don't understand though is that if I use this API Key, it's going to be pointing to my newly created "App", which has no fan base. How do I link this API Key, (or setup another key through the page admin), so I can have users "like" the real facebook page?
I want to run some javascript functions the moment a user likes the page, but I'm also a little confused on what API functions call, and whether these return a true/false value? I only really want to run these js functions if the user has not already liked the page..
Hope this all makes sense, would love any explanations you have to offer to point me in the right direction.
From what I can see, the answers on your other question cover most of what you need to know. The one thing I notice is your comment:
"I am attempting to set a promo code in the background when someone
"likes" the page"
There are some tricky terms and conditions surrounding this. Have a look here before continuing: https://developers.facebook.com/docs/guides/policy/examples_and_explanations/Rewarding_Users/
If you start off with adding the like button, then separately you will need to check each logged in facebook user to see if they are connected with your page. You can do this using the api call to get their likes, and checking for your page id in the response:
FB.api('/me/likes', function(response) {
console.log(response);
});
If you find a match, proceed with your promotion, else show the like button.
What you're trying to do is totally possible, although I usually do that calculation server-side. You may be able to do it via the Javascript SDK, using the basic concept below. Check out this link: http://fbmhell.com/2011/06/facebook-like-gating-in-iframe-tabs/
The basic overview is this:
You create a Page
You create an app for your promo tab
You add the app tab to your page
When the user hits your app tab on your page, Facebook will return a signed request to you.
You can parse out that signed request using a function like this:
function grokSignedRequest() {
if (isset($_REQUEST['signed_request'])) {
$encoded_sig = null;
$payload = null;
list($encoded_sig, $payload) = explode('.', $_REQUEST['signed_request'], 2);
$sig = base64_decode(strtr($encoded_sig, '-_', '+/'));
$data = json_decode(base64_decode(strtr($payload, '-_', '+/'), true));
return $data;
}
return false;
}
As mentioned in that article, if you do a print_r() on that signed request after it's been run through the function provided, you’ll see something like this:
stdClass Object
(
[algorithm] => HMAC-SHA256
[issued_at] => 1307627872
[page] => stdClass Object
(
[id] => 116633947708
[liked] => 1
[admin] => 1
)
[user] => stdClass Object
(
[country] => us
[locale] => en_US
[age] => stdClass Object
(
[min] => 21
)
)
)
From there, you can access the liked parameter, and display content based on whether or not the user has liked the page.
// call the function to parse the signed request
$sr_data = grokSignedRequest();
// check like status
if ($sr_data->page->liked==1) {
echo 'you are a fan';
} else {
echo 'you are not a fan.';
}
// check admin status
if ($sr_data->page->admin==1) {
echo '<li>Dude, you are an ADMIN! BADASS!';
}