Facebook web application getting Acess Token - facebook-graph-api

I am creating a web application using php. I have a closed group in facebook. When a user login to my application I am showing them a page. I want to stream data grom my facebook group and display in that page. Since this is a closed group I am not able to stream data from that using php SDK. I am using the following code.
This code workes if the group is public.
$facebook = new Facebook(array('appId' => FB_APP_ID, 'secret' => FB_SECRET_KEY, 'cookie' => true,));
return $facebook->api('/' . FB_GROUP_ID . '/feed');
I think that we have to pass access token with extended permission to get that data. But how to generate that token without user intevention ?
Any Ideas?

Related

Acquiring Long Lasting Facebook Token

Through Facebook's PHP SDK getting started guide it takes you through the process of initializing your app, creating a Login URL and then handling the call back data and doing a simple query.
In the documentation it says you can skip the initializing process providing an access token from 'some other means'.
$session = new FacebookSession('access token here');
Due to the lack of documentation I'm struggling on how I would define a scope before creating the login URL and then use the call back data to extract the access token.
This tutorial will help you get started with using the Facebook PHP SDK to log a user in.
Basically, you need to setup your application first and use the FacebookRedirectLoginHelper to create the login URL:
FacebookSession::setDefaultApplication( 'xxx','yyy' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://yourwebsite.com/app/' );
// show login url, scope is array of permissions
echo 'Login';

Facebook SDK PHP: return friends details

I have a web service that servers Android, IOS and Windows Phone. Each app connects to Facebook using native SDK and passes the fb_user_id and access_token when performs API's requests. The API should retrieve the user and friends details and save them on MySQL.
The system works fine, except that it can't return friend's retionship, location and birthday.
$fb = new Facebook([
'appId' => my_app_id,
'secret' => my_app_secret
]);
$r = $fb->api("$fb_user_id/?fields=friends.fields(relationship,birthday,location)&access_token=$access_token");
var_dump( $r );
My app on Facebook has friends_relationships, friends_location and friends_birthday permissions. When we test using a Facebook user setted as developer, the app returns all the information, but when a non-developer uses we can't return those informations.
Any idea?
I figured what happened!
Facebook's SDK generates a token for /me. In PHP I was calling fb_user_id?fields=friends.fields(id,name,birthday,etc). But requests with /fb_user_id can't return info with a /me generated token.
It works when used directly on browser https://graph.facebook.com/me?fields=friends.fields(id,name,birthday,etc), but fails when called by Facebook's PHP class:
An active access token must be used to query information about the current user.
My solution:
$url = "https://graph.facebook.com/me?fields=id,name&access_token=$this->access_token";
$this->me = file_get_contents ( $url );
$json = json_decode($this->me, true);
For further information, checkout this answer. New ideas are welcome.

arguments when using the PHP Facebook API Graph library?

I am trying to pull some statuses in json format from Facebook's Graph API. Since i am using PHP, so I went for the following library.
https://github.com/facebook/facebook-php-sdk/blob/master/examples/example.php
I saw the example, and the class requires some arguments upon instantiation, and I just don't know what they are:
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '344617158898614',
'secret' => '6dc8ac871858b34798bc2488200e503d',
));
And also from the Graph API Explorer I see the access_token, but this PHP library doesn't mention it at all, I thought I need the access_token to make requests, don't I? I hope someone can clear things up for me.
Yes you require an access_token to query on behalf of user on Facebook, it can either be an App access token or User access token. If the user authorizes your app, with required permission which in your case would be read_stream permission, then you can obtain his access token and query for status updates. The default access token is your App access token which can access only public data.
Also I would suggest you to to over the PHP SDK documentation for getting started with it.

Facebook API partially working in production

I´m using Facebook graph API thru the JS SDK and the Php SDK. While all is working in development environement, in production, on a shared hosting, it is partially working and this is quite disturbing.
First, the facebook app is exatly the same between tests in dev and tests in prod, I´ve just changed the domain.
Second, outbound ports 80 and 443 are opened on my hosting provider.
Dev : windows, php 5.3.8, curl 7.21.7
Prod: Linux, php 5.3.17, curl 7.24.0
I´m presenting here under the results of my tests.
Dev
Publish a message in user : OK
Publish a message in page : OK
Publish a photo in user : OK
Publish a photo in page : OK
Delete a message in user : OK
Delete a message in page : OK
Delete a photo in user : OK
Delete o photo in page : OK
Prod
Publish a message in user : OK
Publish a message in page : OK
Publish a photo in user : OK
Publish a photo in page : OAuthException: An unexpected error has
occurred. Please retry your request later.
Delete a message in user : not tested
Delete a message in page : OAuthException: (#100) Invalid parameter
Delete a photo in user : OAuthException: (#221) Photo not visible
Delete a photo in page : unable to test
Does any one have a clue on this ? How can I debug what is going on (I on a shared hosting provider) ?
EDIT
This is the code to publish a photo on a page which is KO in prod
// I´m using Yii framework with a wrapper around Facebook php sdk
// retreiving the page acces token
$page_at = Yii::app()->facebook->api('/'.$fb_page->page_id.'?fields=access_token');
Yii::app()->facebook->setFileUploadSupport(true);
// model contains the post data from the user. For the message it is a text area
// Yii::app()->params->uploadPath is a global param with path where the image resides.
// Directory separator where created with php´s DIRECTORY_SEPARATOR
$data = array(
"message" => $model->attributes['descricao'],
"source" => "#".Yii::app()->params->uploadPath.$model->image,
"access_token" => $page_at['access_token']);
$result = Yii::app()->facebook->api('/'.$fb_page->page_id.'/photos', "POST", $data);
This is the code to publish to a user which is OK in prod
Yii::app()->facebook->setFileUploadSupport(true);
$data = array(
"message" => $model->attributes['descricao'],
"source" => "#".Yii::app()->params->uploadPath.$model->image,
);
$result = Yii::app()->facebook->api('/'.$fb_page->page_id.'/photos', "POST", $data);
The only difference between the two is that I don´t need the access_token for the user
EDIT
The delete problem seems to be resolved by putting the page access token in the url instead of putting it in the post. Strange since I remember I was forced to put it in the post in my dev to make the delete work...
Now I´m focused on the image post for which I´m getting the same behavior after a zilion different combinations. The API call is doing something despite the error : it creates the app album if it does not exists but the image is not uploaded.
Could it be related to this bug : https://developers.facebook.com/bugs/376442565760536?browse=search_5099ba94055685677909148
EDIT
Following cpiko advice I´ve worked on CRLF potential problems. I´ve set CURLOPT_CRLF to true. Now I get : OAuthException: A user access token is required to request this resource.
EDIT
After fighting a little with fiddler I could compare the facebook api curl request sent between dev and prod. Well.... absolutly identicals. I´m starting to be out of ideas...
Well after so much tests, what was blocking is the Country restriction setting in the facebook app advanced settings.
The problem is that my hosting provider is in the US and I restricted the facebook app to my country (which is not US). What is strange is that this setting only applies to photo upload in my case and not the rest (feed posting for example). Also, it does not restrict based on the client (browser) origin but in this case fron the server originating the curl.
UPDATE
The error upon deleting a user photo (Photo not visible) was different. Even if I was the publisher of the photo thru my facebook app, I had to add the user_photos permission so I could delete it

FaceBook downloading photos using php-sdk

I am new to Facebook PHP SDK for the Graph API.
I have created an application on Facebook and I got the application id, secret ..etc.
I also got the access code with some permissions using the scope parameter.
Using that code I got the access token with offline access.
https: //graph.facebook.com/oauth/access_token?client_id=xxxxxxxxxx&redirect_uri=http://192.168.15.5/xxxx/facebook/&client_secret=xxxxx&grant_type=client_credentials.
Using this I got the access token.
Now my Question is that I want to access the user information such as photos and friend lists
I do not know if it is correct or not,
https://graph.facebook.com/me/friends?access_token=xxxxxxxxxxxxxxxxxxxxx
when I use this it says,
"{
"error": {
"type": "OAuthException",
"message": "An active access token must be used to query information about the current user."
}
}"
How can I access the user information, by using the access token of that user.
I want to access the user information from the application at anytime for the user who has allowed my application to access his information.
If you use the Facebook PHP SDK (see on github), you should not call these URL directly but only call functions of the SDK.
See this answer to see how to fetch and use the offline access token to make API calls : How to login with offline_access using the new Facebook PHP SDK 3.0 ?
Then to get the user friends and their photos, you will call :
$args = array("fields" => "name,picture");
$friends = $facebook->api('/me/friends', $args);
Then the array $friends['data'] will contain the URLs of the pictures of the friends :
Array(
[0] => Array(
[name] => Quentin Pleplé
[id] => 1536397056
[picture] => http://profile.ak.fbcdn.net/...jpg
)
[1] => ...
...
)
Hope that helps !