Facebook graph api - facebook-graph-api

how to give publish_stream permission?If i trying to post on own wall,still it is saying that user is not authorised the application to do this action.I don't have any app.

There are a few ways to do it. Personally I would use the Javascript SDK.
Another way is to update your Facebook App settings (using Facebook Developer App) to require users to permit Extended Permissions 'publish_stream'.
Below is code using the Javascript SDK FB.api function:
FB.login(function(response) {
if (response.authResponse) {
// user is logged in and granted some permissions.
console.log("user is logged in and granted some permissions.")
} else {
// User cancelled login or did not fully authorize.
console.log("he failed to login or something")
}
}, {scope:'publish_stream'});
Inside scope: you can put a comma-separated string of permissions, if you want to ask for multiple.

Related

Can I access Facebook's friend list and other information using AWS Cognito?

We are analyzing AWS Cognito adoption as our authentication service.
We use social network data in some ML models after the user is logged in, if he or she allowed us to.
The doubt is: if the user signs up using Facebook, can I ask for custom information (friends list, list of posts, etc.) using Cognito? If yes, how would I access this information later?
We searched everywhere but can't find anywhere talking about it.
For anyone arriving by Google: the Cognito SDKs return a response that contains the API key for that user on Facebook. Check it out in the Javascript SDK:
FB.login(function (response) {
// Check if the user logged in successfully.
if (response.authResponse) {
console.log('You are now logged in.');
// Add the Facebook access token to the Cognito credentials login map.
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'IDENTITY_POOL_ID',
Logins: {
'graph.facebook.com': response.authResponse.accessToken
}
});
// Obtain AWS credentials
AWS.config.credentials.get(function(){
// Access AWS resources here.
});
} else {
console.log('There was a problem logging you in.');
}
});
response.authResponse.accessToken is the token you can use in the Facebook API. You set the permissions you need from this user in your app in Facebook's developer console. Then, you can proceed to make API calls to Facebook.

Facebook server-side login, CORS

I'm implementing a web site with FB server-side login as simplified steps below:
A simple button triggers JS script which calls my backend API https://localhost/fblogin
function sendFbLoginData() {
$.get("https://localhost/fblogin", function(data, status) {});
}
In the backend handler of /fblogin the user is redirected to FB login dialog for requesting permissions and access token.
func (ct *LoginController) FbLogin() {
url := "https://www.facebook.com/dialog/oauth?client_id=xxx&redirect_uri=https://localhost/fboauth2cb&response_type=code&scope=public_profile"
ct.Redirect(url, 302)
return
}
At browser console shows error msg:
XMLHttpRequest cannot load https://www.facebook.com/dialog/oauth?client_id=xxx&redirect_ur…e_type=code&scope=public_profile. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://localhost' is therefore not allowed access.
After googling I realize this is a CORS problem. Since I cannot change Facebook's behavior, how do I deal with this problem? or fundamentally I do fb server-side login in a wrong way?
ps. my env is AWS + Beego (golang)
You cannot ask for https://www.facebook.com/dialog/oauth?... from JavaScript and wait for its results; that page is intended to be shown in a browser, so that Facebook can ask the user for its credentials and permission to use his account in your app.
So, instead of:
$.get("https://localhost/fblogin", function(data, status) {});
you should use something like:
location = "https://localhost/fblogin";
in your app settings in Facebook in settings tab (somewhere like https://developers.facebook.com/apps/{app-id}/settings/)you should set your site URL to https://localhost/ so it can recognize it as a valid redirect

Using oauth2 service (like facebook) to authorize my app access to third party app

I'm using facebook connect to authorize users on my site, but I also popup a third party service for the users.
This third party service is also using facebook connect to authorize its users.
I don't want each user to be forced to go through the authorization phase twice.
Is there a way I can ask the user to authorize the third party service when he log-in my service get some token and use it when I pop-up the third part service?
You can use Facebook SDK's getLoginStatus method to determine if the user has already authorized your app and is currently logged in. If, user logs in to your site using FB, he will not have to log in again.
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});
So, the user just logs in once and has to authorize the app for the first time only. You can read more about this method here.

Graph API Javascript bug

Recently I am experiencing this problem.
When I click to register with facebook and I am logged in as a user, I can see my information correctly. But staying on the same page, if I log out from facebook in some other tab and click Register with facebook on my own site, I see this person profile picture. The url of this image is:
http://graph.facebook.com/undefined/picture?type=large
I am accessing graph API using APP
Kindly advise me the solution to the problem
Accessing a users data requires an access token, with out this token your call is getting undefined returned as a user, and with out the access token appended to the picture url it returns emphamous Unknown User.
refer to: http://developers.facebook.com/docs/facebook-login/access-tokens/
for requesting access tokens, and usage.
Example Only: https://graph.facebook.com/ShawnsSpace/picture?type=large&return_ssl_resources=1&access_token=users_access_token
My script cannot access FB script without Access token
FB.login(function(response)
{
//If the user is succesfully authenticated, we execute some code to handle the freshly
//logged in user, if not, we do nothing
if (response.authResponse)
{
FB.api('/me', function(response) {}
}
{scope:'email,user_events,friends_photos,user_about_me,user_birthday,user_hometown,user_location,user_location,user_relationships'}); });//fbclick
So, I think access token condition is already satisfied.

get facebook extended permission for uploading photos

In my facebook application, when the users first starts using it,the app request very basic permission. At a later stage, I need to request extended permission to allow the app to upload photos, I'm googling when falling only on the legacy part:
FB.Connect.showPermissionDialog("photo_upload",permissionHandler);
Does someone has any idea how to get permission for uploading photos using the new sdk??
There is no photo_upload permission. But, you can upload photos using the publish_stream permission instead. And you can see the user's photos using the user_photos permission.
To obtain extended permission from the user, there are two ways to ask the permission from the user. In either way, the finality is to get the permission from the user:
Opens a popup and ask the user the extended permission
FB.login(function(response) {
if (response.authResponse) {
// user is logged in and granted some permissions.
} else {
// User cancelled login or did not fully authorize.
}
}, {scope:'read_stream,publish_stream,offline_access'});
Open a new window and ask the user the permission
window.location="https://www.facebook.com/dialog/permissions.request?app_id=&next=https://apps.facebook.com//&type=user_agent&perms=user_photos,publish_stream,friends_photos&fbconnect=1";
You also need to know which permission you want from the user. Here is a list of permission that you can ask from a facebook user