"An access token is required to request this resource" while accessing an album / photo with Facebook php sdk - oauth

I am using the php sdk to access a user's albums and photos in a website.
I am able to login and also able to retrieve all the info about the albums and photos.
However, I am not able to display these photos on the webpage.
When I try the graph API, to get the photo / album like below:
https://graph.facebook.com/131833353530147/picture
I get the following error message:
{
"error": {
"type": "OAuthException",
"message": "An access token is required to request this resource."
}
}
But the same is working in while I try to display my profile pic and I do have a valid access token.
Help on this would be greatly appreciated.

There are 3 things you need.
You need to oAuth with the owner of those photos. (with the 'user_photos' extended permission)
You need the access token (which you get returned in the URL box after the oAuth is done.)
When those are complete you can then access the photos like so https://graph.facebook.com/me?access_token=ACCESS_TOKEN
You can find all of the information in more detail here: http://developers.facebook.com/docs/authentication

Try This url with valid userid and access token:
https://graph.facebook.com/{userid}/photos?limit=20&access_token={access_token}

Login to your Facebook.
Go to http://graph.facebook.com
You will find all your feeds with their corresponding access codes. e.g https://graph.facebook.com/me/home?access_token=2227470867|2.AQAQ6FqN8IW-PUrR.3600.1309471200.0-137977022924629|0sbmdhJN6o9y9J4GDWs8xEygyX8
Enjoy!

To get an access token: facebook Graph API Explorer
You can customize specific access permissions, basic permissions are included by default.

To get the actual access_token, you can also do pro grammatically via the following PHP code:
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
$access_token = $facebook->getAccessToken();
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}

Well, you are having a valid access token to access your information and not others( this is because you got logged in and you have given permission to access your information). But the picture owner has not done the same (logged in + permission ) and so you are getting a violation error.
To obtain permission see this link and decide what kind of informations you want from any user and decide the permissions. Later on embed this in your code. (In the login function call)
Thanks

Related

How to authenticate user with token (stay authenticated in iPhone)

I have two related questions and I hope someone help me because I've been stuck for 2 days
First: mobile phone failed to authenticate
Here is what I have done:
1- user signs up
2- token released
3- token saved in user's device
but then when the same user try to do API requests I get
Rooute to sign up :
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', function ($api) {
$api->post('auth/signup', 'App\Api\V1\Controllers\AuthController#signup');
then I get a token , so I guess everything looks great!
then now when the same device sends a post request to laravel I get this message
"message": "Failed to authenticate because of bad credentials or an invalid authorization header."
this is the route to the post request
$api->group(['middleware'=>'api.auth'],
function ($api) {
$api->post('auth/ios', 'App\Api\V1\Controllers\AuthController#create');
Second: is my method right to save data made by a mobile phone?
Since I couldn't test this method I'd like to know if this is at least one of the right ways to receive data and save it. The reason to save it is because I will show it in a control panel.
public function create(Request $request)
{
$user = new User();
$id = Auth::id();
$user->phone = $request->input('phone');
$user->city = $request->input('city');
$user->street = $request->input('street');
$user->save();
return 'Employee record successfully created with id ' . $user->id;
}
I understand that you are authenticate users based on api token.
Here is what you could do :
set up a column called api_token in users table by adding the following migration
$table->string('api_token', 60)->unique();.This generates a random api token for every user.
send the api_token back to the user's device and save it there
Send it back with every request. Preferalbly set it up globally and send it in the request Authentication request header
Get the authenticated user like so$user= Auth::guard('api')->user();
Laravel takes care of all the authentication stuff behind the scenes.
Learn More about this here

What information about a user can I access when I get him/her to login into my appl. through twitter

I want to provide a "Login with twitter" functionality for my web application.When the user logs in, I want to request him to share specific information about his profile and tweets as I want to read that information and build some recommendations based on that. However, I do not know what all information can be had from this twitter for that user. Can you please point me to some code/docs that help me do this.
Thanks
kabir
<?php
require_once('config.php');
// Get consumer key and consumer secret from config file
// retrieve access token stored in database
require_once('twitteroauth.php');
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET );
$response = $connection->get('account/verify_credentials');
// This returns an object with the following info (user account info)
// [https://dev.twitter.com/rest/reference/get/account/verify_credentials][1]
$response2 = $connection->get('statuses/home_timeline');
// This returns an object with the following info (user timeline tweets)
//https://dev.twitter.com/rest/reference/get/statuses/home_timeline
?>

How to get token_for_business for Facebook login - iOS

How can I get token_for_business from Facebook login? I have setup a business and associated my application with that business. Facebook documentation says you need to call GET /me?fields=token_for_business on User node, and it will return following json.
{
"id": "1234567890"
"token_for_business": "weg23ro87gfewblwjef"
}
I tried by providing token_for_business in parameter list of /me call but didn't work. Need advises.
Edit
I found this URL https://developers.facebook.com/docs/graph-api/reference/v2.5/user and tried by passing token_for_business in param of initWithGraphPath but no gain. I checked in Graph API Explorer and getting the required data. But not sure how to call from my objective-C code.
Edit 2
I Inspected the error object of initWithGraphPath:#"me?fields=token_for_business" call and found following details
Error Domain=com.facebook.sdk.core Code=8 "(null)" UserInfo={com.facebook.sdk:FBSDKGraphRequestErrorCategoryKey=0, com.facebook.sdk:FBSDKGraphRequestErrorHTTPStatusCodeKey=403, com.facebook.sdk:FBSDKErrorDeveloperMessageKey=(#200) Application must be associated with a business. https://business.facebook.com/, com.facebook.sdk:FBSDKGraphRequestErrorGraphErrorCode=200, com.facebook.sdk:FBSDKGraphRequestErrorParsedJSONResponseKey={
body = {
error = {
code = 200;
"fbtrace_id" = "EN9bN/YMloA";
message = "(#200) Application must be associated with a business. https://business.facebook.com/";
type = OAuthException;
};
};
code = 403;
}}
But if I try my app in Graph API Explorer then I'm getting the required data. So now it confusing me.

get user likes from FBSDK graph Api (V 2.5)

I am trying to retrieve all pages a user has liked on Facebook in my app. Taking from the Graph API, here is my code:
let request3 = FBSDKGraphRequest(graphPath: "/me/likes", parameters: ["fields": "id,name,picture, user_likes"])
request3.startWithCompletionHandler({ (connection3, result3, erreur3) -> Void in
if erreur3 == nil {
print(result3)
}
else {
print(erreur3)
}
})
this code yields this:
{
data = (
);
}
I also tried "me/likes", and it gave me the same empty data set. I tried some of the answers here Facebook Graph API - likes returns me an empty set as well but none of those worked.
I have the "user_likes" permission, and I have a valid AccessToken as well.
Also, I have set my Facebook likes to public, so I know they aren't being hidden because of privacy settings. Any idea how to get the user likes?
thanks
See this is screenshot i got data ..
if you have don't permission on graph explore then you don't get any result..
in a development you add permission on graph explorer then after when you live your app you need to submit your app for permission.
if you have any query then tell me i will help you.

Facebook get number of likes on custom object without login

I am using og.likes to like custom object from my app, with the help of Facebook-iOS sdks. I am able to like object and also read the like.
Now I want to know if there is any way in graph API that, I can show total number of likes on an object to a user who is not logged in to our app via Facebook ? i.e without access token
As per FB developer docs:-
https://developers.facebook.com/docs/graph-api/reference/v2.3/object/likes
It is mentioned that you need the same permissions required to view the parent object are required to view likes on that object, means an user access token with read_stream permission is required.
NOTE:
But if a user want to get the no of likes for Facebook page or profile or object , then it can be easily done by this:-
https://graph.facebook.com/< your object id>/
Then you will receive json response with Like Count:-
{
"id": "12345",
"link": "http://www.facebook.com/pages/MY iOS Page/102445859460201",
"likes": 150,
"type": "page"
}
Code:-
let url = NSURL(string: "http://graph.facebook.com/"+ObjectID)
let urlRequest = NSURLRequest(URL: url!)
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue()) { (response:NSURLResponse!, data:NSData!, error:NSError!) -> Void in
// Fetch the data
var jsonDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSDictionary
let likesCount = jsonDictionary["likes"] as? String
}
Got Response from Facebook Team :
Since Graph API v2.0, all requests to the API require an access token.
You can proxy this request through a web-server and use your app
access token for example.
You could as well make the request from your app by shipping your app
access token with the app, but I highly advise against this as the app
token should be kept secret.
Please note that the SDK does not support using the app access token
for security reasons. So you have to write your own networking code to
fetch this data.
I got the solution from
How to get Likes Count when searching Facebook Graph API with search=xxx
Here in this link it's not the exact solution but i have tried my way and unfortunately it worked for me, and i am quite satisfied with it, still using it in my application.
https://graph.facebook.com/v2.1/{Object-id}?fields=likes.summary(true)
You will need an access token, no need to login any user, you can generate an APP Access token to get that info .
jQuery.get("https://graph.facebook.com/oauth/access_token", {
'grant_type': "client_credentials",
'client_id': "000000000000000",
'client_secret': "0000000000000000000000000000000000"
}, function (data) {
// data is the app access token you will need for calling graph api
});
Thats more complicated but solves the necessity to login the user to get public informations from the graph .

Resources