revokeApplication Using Facebook's Graph API - facebook

I'm in the final stages of converting our site over to Graph API from the Rest API.
The last piece I'm missing is the old "revokeApplication" call used for when a user chooses to "remove connection" from our site.
Despite my desires to completely remove the Rest API, I thought I might just fire it up for this, but it requires a session key -- something no longer stored in the Graph API.
Anybody have any ideas?

I figured it out. I'll leave it here for those that need to know...
The old rest api (including the revokeApplication api) can still be accessed, now with the new OAuth access_token. Just use this url: https://api.facebook.com/method/METHODNAME
For this particular call, it's a POST:
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, 'access_token='.$users_access_token);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, 'https://api.facebook.com/method/auth.revokeAuthorization');
$output = curl_exec($ch);
curl_close($ch);
More info here:
http://developers.facebook.com/docs/reference/rest/

You can do it with the new graph API :
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true
));
$revoked = $facebook->api("/me/permissions", "DELETE");
$revoked is a boolean.

Related

Sendgrid Unsub For Only Specified Group

I am using sendgrid unsub api's for who does not want the campaign e-mails.
But when I send request for unsub the email, the adress goes to global unsub list.
Here is my php code
$parameters = array(
"api_user" => self::EMAIL_USER_NAME,
"api_key" => self::EMAIL_PASSWORD,
"email" => $email,
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters);
$json_response = curl_exec($curl);
How to specify an unsub group id for api call?
It looks like you're using the V2 API right now. Unsub groups are only available via the V3 API, using the suppressions endpoint.
In V3, you POST addresses to the resource URL of the unsub group, for example, https://api.sendgrid.com/v3/asm/groups/:group_id/suppressions
Keep in mind that to use V3 you'll need to use the updated authentication schema that uses an Authorization header and a real API key.

Access token for a simple FQL retrieving the number of status for a page

I'm doing a really simple PHP app using the latest Facebook PHP SDK which aims to display the number of status a page of mine has.
To do so I created an app to have the app id and the app secret but after I'm kinda lost.
I thought I needed an app secret token so I first tried like this:
<?php
public function getFacebookPosts() {
require __DIR__ . '/libs/facebook-sdk/facebook.php';
$appId = 'myID';
$appSecret = 'mySecret';
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret,
));
$token = $this->getFacebookAppToken($appId, $appSecret);
try {
$jinnove = $facebook->api('/my.page');
$fql = '/fql?q=SELECT+status_id+FROM+status+WHERE+uid=' . $jinnove['id'] . '&' . $token;
var_dump($facebook->api($fql));
} catch(FacebookApiException $e) {
var_dump($fql, $e);
}
}
/**
* Function to Get Access Token from Facebook
* #param $appId
* #param $appSecret
* #return string
*/
protected function getFacebookAppToken($appId, $appSecret)
{
$args = array(
'grant_type' => 'client_credentials',
'client_id' => $appId,
'client_secret' => $appSecret
);
$ch = curl_init();
$url = 'https://graph.facebook.com/oauth/access_token';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($ch);
return $data;
}
But it returns me an error 102 with the following message: "A user access token is required to request this resource.".
So then I asked on IRC and someone told me I need a user access token to do that.
Of what I've understood a user access token can only be generated when a user explicitly log into facebook to authorize this app and renew the token sometimes.
Is that true? Is there no way to use a token which doesn't imply the user to be logged? Basically anyone can view this number of status, even people who don't have a Facebook account and I want no UI dialog at all.
For some reason, Facebook has decided that querying a status can only be done by a user.
You can get around this by querying the stream table, and only returning posts with type = 46, which are status updates:
SELECT post_id FROM stream WHERE source_id= YOUR_PAGE_ID AND type = 46 LIMIT 100
The stream table has a lot of restrictions on it. Even with a high LIMIT, you may not get all the status updates if the page has been around for a while.
You can also speed up your program by cutting the number of API calls from 3 to 1 with the following changes:
If you want to get a page's ID from its username replace = YOUR_PAGE_ID in the above query with IN (SELECT id FROM profile WHERE username = 'YOUR_PAGE_USERNAME')
You don't need the 'getFacebookAppToken()` function. The PHP SDK will automatically get you an app access token if you don't have an authenticated user.

CURL communication with Facebook's graph not working

everyone.
I have the following issue:
I'm using curl to get some info from facebook's graph (and this used to work until just a few days ago), but now I just get an empty answer.
The request is quite simple:
https://graph.facebook.com/?ids=XXX&access_token=YYY
The ids parameter is just a list of ids for elements in the graph (in this case, application requests). When I copy/paste the url on a browser, it works, but when using curl it gets stuck without an answer.
The full code for the curl call is:
require 'php/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXX',
'secret' => 'YYY',
));
$url = "https://graph.facebook.com?ids=".$_POST['data']."&access_token=".$_POST['access_token'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$results = curl_exec($ch);
curl_close($ch);
echo $results;
Could anyone shed some light on this?
Cheers!
try {} graph.facebook.com/ with trailing slash
$url = "https://graph.facebook.com/?ids=".$_POST['data']."&access_token=".$_POST['access_token'];
also i am unsure where you are trying to retrieve the post from so you could try request method instead.
$url = "https://graph.facebook.com/?ids=".$_REQUEST['data']."&access_token=".$_REQUEST['access_token'];
example ajax call to php:
// get albums
function showAlbums(pageid,limit,offset){
thealbums = "albums";
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("albums").innerHTML=xmlhttp.responseText;
}
};
xmlhttp.open("GET","plugins.albums.php?pageid="+pageid+"&limit="+limit+"&offset="+offset+"",true);
xmlhttp.send();
}
Try for cURL:
function GetCH(){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com?ids=".$_POST['data']."&access_token=".$_POST['access_token']");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT_MS,20000);
if(substr($url,0,8)=='https://'){
// The following ensures SSL always works. A little detail:
// SSL does two things at once:
// 1. it encrypts communication
// 2. it ensures the target party is who it claims to be.
// In short, if the following code is allowed, CURL won't check if the
// certificate is known and valid, however, it still encrypts communication.
curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_ANY);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
}
$sendCH = curl_exec($ch);
curl_close($ch);
return $sendCH;
};
$ThisId = GetCH();
echo $ThisId;

Facebook Graph API - Photo Upload

I have an application that uses the old Facebook API but now I'm migrating it. The application works good until I try to upload a photo.
I knew how to do it in the old way, but now... I'm in troubles.
This is the way I used to do it:
$args = array
(
'method' => 'photos.upload',
'v' => $ver,
'api_key' => $key,
'uid' => $uid,
'call_id' => $cid,
'format' => 'XML',
'caption' => $caption
);
signRequest($args, $sec);
$args[basename($file)] = '#' . realpath($file);
$ch = curl_init();
$url = 'http://api.facebook.com/restserver.php?method=photos.upload';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
Any ideas??
Thanks
The API url starts with https:// not http://. That could be the issue.
I found the solution here:
Uploading a picture to facebook
There is shown how to use the new Facebook Graph API with the PHP Curl function and a valid session token.

User must have accepted TOS - Facebook Graph API error when posting photos to group page

I've been struggling to upload an image from the user's computer and posted to our group page using the Facebook Graph API. I was able to send a post request to facebook with the image however, I'm getting this error back: ERROR: (#200) User must have accepted TOS. To some extent, I don't believe that I need the user to authorize himself as the photo is being uploaded to our group page. This below, is the code i'm using:
if($albumId != null) {
$args = array(
'message' => $description
);
$args[basename($photoPath)] = '#' . realpath($photoPath);
$ch = curl_init();
$url = 'https://graph.facebook.com/'.$albumId.'/photos?'.$token;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$data = curl_exec($ch);
$photoId = json_decode($data, true);
if(isset($photoId['error'])) die('ERROR: '.$photoId['error']['message']);
$temp = explode('.', sprintf('%f', $photoId['id']));
$photoId = $temp[0];
return $photoId;
}
Can somebody tell me if I need to request extra permissions from the user or what i'm doing wrong?
Thanks very much!
Actually, I never succeeded in this :(. As a work around, we created a new facebook user instead of a group page.
This is a known bug and it looks like they're working on it:
http://bugs.developers.facebook.net/show_bug.cgi?id=11254