Facebook Login For Rest Api - javascript

I am trying to implement Facebook login SPA. I am using the JavaScript SDK for the login. Fb authorized the user and token is received. Token is send to the web API. When API try to fetch the data from the token only Facebook id and name return from the Facebook graph api. Even though I have set the scope for the different data in java script sdk. If i fetch the data using javascript sdk. In that case i am getting the complete data whatever in the scope.

I think your asking "why am I not getting all the data I requested?". If so the most common reason is because permissions aren't set. For instance if the user grants permission to "public_profile" then Facebook will authorize your app to obtain every field that that the "public_profile" permission permits, in this case the following:
id, name, first_name, last_name, age_range, link, gender, locale, picture, timezone, updated_time, verified
obtained via
FB.api('/me',
'GET',
{"fields":"id,name"}, //any of the other fields here
function(response) {
// Insert your code here
} );
however if added 'email' in as a field then the Facebook would return everything except the 'email' because there a permission specifically for granting access to the users email called email. You can see what permissions enable what data returns at https://developers.facebook.com/docs/facebook-login/permissions
and you can test it at https://developers.facebook.com/tools/explorer. I hope that's what you where trying to say in your question. Cheers!

Related

How can I set custom signatures across a domain Gmail API

I want to set the signature for all users on my Google Apps domain to their name and job title.
Through my research, I found that the only free way to do this is by using the Gmail API.
I managed to get a list of all users on the domain (with the required info, email address that I want to set the signature of, name, job title) simply by using the quick start guide at https://developers.google.com/admin-sdk/directory/v1/quickstart/js.
At the end of the listUsers function I added the loadGmailApi code.
Once the api is loaded, it runs a funtion called setSignatures.
function setSignatures() {
var request = gapi.client.gmail.users.settings.sendAs.update({
'userId': email,
'sendAsEmail': email,
'signature': '<strong style="color: rgb(230, 145, 56); font-family: arial, sans-serif; font-size: large;">' + name + '</strong>'
});
request.execute(function(resp) {
console.log(resp);
});
}
This works perfectly when the variable "email" is my own, but when it is anyone elses, I get the following error: 403 Delegation denied for 'myemail'. I am a super admin.
I now found out I need to use a service account.
I then completely followed this guide How to obtain Google service account access token javascript.
The one difference is that to authorise I use
gapi.auth.authorize({
instead of
gapi.analytics.auth.authorize({
As I don't need the analytics API. I hope that isn't causing problems. I need the directory and gmail API.
At the end of the code provided in the above link, I loadDirectoryApi() and run code to get all the user information again, as before. I can't even get to setting the signatures, because at this point, I get an error saying : 401 Login Required.
So if I use my own email to try this, I get a 403, and when I tried a service account, I got a 401. It says login required, how can I log in? I thought service accounts don't actually log in, but I use a JWT to make an access token.
I am authorising by
gapi.auth.authorize({
'serverAuth': {'access_token': token},
'scope': 'https://www.googleapis.com/auth/admin.directory.user',
'client_id': 'myClientID'
});
What am I doing wrong? Please help. I need to know how to "log in" or authenticate with a service account so that I can update signatures.
Even if you are super admin you cant change email signatures of other users by default using gmail api.
First you need to create a service account in google dev console and add domain wide delegation for that account.
After that you go to your google admin and add that service account with required scopes under API control.
Then you need to use those credentials inside of your python script.
Basically what it does is, it allows you to impersonate as other user and change email Signature. You don't need password as the service account has domain wide rights. The below link explains how to do that.
But the link describes it using PHP but still it will help you to create a domain wide delegation service account.
https://moometric.com/integrations/gsuite/use-gmail-api-update-signatures-gsuite-users-php/

Facebook api call

I am creating a website in PHP and i want to use login with Facebook in my website. I have created an app ID in Facebook developer. Facebook login is working but i want to retrieve email id which is used on Facebook login.
here is my JavaScript code :
$facebook.api("/me").then(
function(response) {
console.log(response);
alert("Welcome " + response.email);
},
function(err) {
alert("Please log in");
});
Output:
response.email is undefined.
On console log i am getting an object like object {name:"xxx",id:"111"}
How to retrieve Facebook login email id?
Facebook provides all the details which are public or approved by facebook. So in that case you cannot get email-id everytime.
Suppose one user is not using email in his account or email is private, than you cannot get his email-id.
Regards.
In order to get the user email, you will need to follow these steps:
Set the email permission for your app.
When you call $facebook.api("/me") try calling like this $facebook.api("/me?fields=id,name,email") by doing this you should get the email of the logged in person.
Note:
Even if you have set the email permission some user sign up for facebook using their mobile no. as username or some user want their email to be private by setting privacy, so, in that case you won't get the get the user email.
So in case you want to save user in database for future reference work on id as this is the unique identifier for that user.
In case you want user email and if fb doesn't provide it you can ask explicitly.
Hope this will help you.

How do i get the recent company updates from linkedin using OAuth2

I am creating a html page were am trying to get the recent updates about the company from following REST call :
http://api.linkedin.com/v1/companies/1441/updates?oauth2_access_token={access_token}
I am getting reply as invalid access token , I have created a new application on linkedin developer network obtained the accesstoken and i am supplying the same access token in the REST call to get company updates .
Can i please know how do i get recent company updates from linkedin using OAuth and bind the output data to some placeholder on html page.
The 'OAuth User Token' of the applicaiton in the LinkedIn Developer site is NOT the oauth2token. To get the oauth2token you have to authenticate (as a user) following this guide: https://developer.linkedin.com/documents/authentication
Good luck! :)

Facebook API - access token works for SOME requests

This is driving me totally crazy. I've set up an FB app and retrieved an access token by visiting: https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=CLIENT_ID&client_secret=CLIENT_SECRET.
Now I want to grab a (publicly available) feed from this URL: https://graph.facebook.com/PAGE_OR_USER_ID/feed?access_token=ACCESS_TOKEN
It works fine when I try it with my own user ID or even when I try another random page but it DOESN'T work when I enter the PAGE_ID I need it to work with.
Here are some examples:
This works fine (a friend of mine's Facebook page):
https://graph.facebook.com/tatchit/feed?access_token=213451182120494|JgAwuCE74lh51t1pKMuRM2mz3GU&limit=10&offset=0
This also works fine (my own Facebook ID):
https://graph.facebook.com/al.dev.7/feed?access_token=213451182120494|JgAwuCE74lh51t1pKMuRM2mz3GU&limit=10&offset=0
This doesn't work (the client's Facebook page):
https://graph.facebook.com/142616539131188/feed?access_token=213451182120494|JgAwuCE74lh51t1pKMuRM2mz3GU&limit=10&offset=0
I don't get any errors but the result is empty.
If I try the Graph API Explorer it also works fine:
https://developers.facebook.com/tools/explorer/?method=GET&path=142616539131188%2Ffeed
But not if I enter my own access token. From what I understand the access tokens you get in the API Explorer are temporary so I don't want to use that.
How can this be?
Also. I've struggled to understand this whole access-token, client-id, app-id-business for ages now - is there a good tutorial where all this stuff is explained? I've never had this work without problems ever.
Thank you
You should be using user access token for all the above queries. App Access token is used when you want to do something like check the app insights so and so.
When the pages has age restrictions or something so, app token wont be able to get through, but since the user is already a member of the page, you can use the user access token to get the data.
Add : Quoting from the documentation,
Note that the **app access token** is for publishing purposes permitted by the publish_actions and publish_stream permissions. You will be **unable to retrieve information** about the status update post with the given ID using the app access token. Instead, you should use a **user access token** for such purposes.
Other Capabilities of an App Access Token
There is a limited set of information that can be retrieved from Facebook using an App Access Token.
Basic Profile Info of a User (ID, Name, Username, Gender)
A User’s Friends and their IDs
Permissions granted by the User to your App
Read : https://developers.facebook.com/docs/opengraph/using-app-tokens/
So you should be querying with the user_access token to access all the informations that you are looking for, not with an app access_token.
You are using an App Access token instead of a User Access token. Some of the pages you are viewing may have a country or age restriction set, so using an app token will not work.
See: http://developers.facebook.com/docs/concepts/login/access-tokens-and-types/

Using Facebook Javascript SDK to get Graph Data

FIXED NOW! But I can't answer my own question yet. See my comment below. And thanks for helping.
I've searched and searched and read the docs and still can't figure this out.
I have a web page about an event. There's also a public Facebook "event" for my event. I'm trying to use the FB Javascript SDK to get the number of attendees for the Facebook event and add it to the number of people who've registered through the website.
I've created an app and I have an appID and secret string. I can get an access token from:
https://graph.facebook.com/oauth/access_token?client_id=XXXX&client_secret=XXXXX&grant_type=client_credentials
and I can then use that access token to get the attendees for a public event:
https://graph.facebook.com/331218348435/attending?access_token=XXXXXXXXX
That's all fine.
I'm now trying to do this same thing using the Javascript SDK.
I've loaded the SDK and done an init:
FB.init({
appId : 'XXXXXXXX',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
and I know the SDK is working because I can get an object with the data that doesn't need an access token:
FB.api( '/331218348435', function (response) { console.log ( response ) } );
but when I try to get the attendee data that needs the access token:
FB.api( '/331218348435/attending', function (response) { console.log ( response ) } );
I get an OAuthException: "An access token is required to request this resource."
All the tutorials and information I can find all refers to using the .login method, but I don't want a user to login, I want to login using my app ID without any user interaction.
I'd assumed that the API took the SDK's init request and granted me an access token when I called the .init method, the authentication being done against my website's address (the HTTP referrer - yes I have set my website URL in the Facebook app settings).
Any ideas what might be causing this to not work? How can I get the access token using the Javascript SDK without doing a .login? Am I missing a step? Is this even possible?
Thanks
Form what the rather circular documentations says, getting the attending feed requires a 'generic access_token`. In Facebook terms:
Any valid access_token
Any valid access token returned by our APIs. An access token may not be valid if, for example, it has expired. No special permissions are required. Occasionally, this is referred to as a generic access_token.
So this means that you can use any token you like to access the attending feed, as long as the event is public. The easiest access token to get seems to be an app token: http://developers.facebook.com/docs/authentication/#applogin. You can get this token using only your App ID and Secret, and no user interaction is required.
To summerise the link content: You can get an application access token by sending a GET request to
https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&grant_type=client_credentials
You can then use that access_token to make the call for you attending list
FB.api('MyEvent/attending?access_token=ACCESS_TOKEN');
Oh. OK.
Well, for whatever reason, I went away and had my dinner and when I come back it's working fine.
When I updated the settings for my app Facebook said it might take a few minutes for the change to get around the servers. Turned out to take over an hour!
My code was fine.
Thanks for your help.

Categories