I want to create a very basic C# application that can fetch publicly-available information for Facebook user profiles. I've read a few pages of Graph API but there is a problem. Here you can see a user profile that contains all sort of information for public:
http://tr-tr.facebook.com/people/A-Altan-Yurt/100002356385895
However I cannot get all of these information, using the Graph API with this link:
https://graph.facebook.com/100002356385895
I tried scraping the page but the returned document doesn't contain any useful information. Since all these info are public, I don't think there should be an access limit for them. What am I doing wrong here?
Yes, even public accessible data can not just be fetched outside of the facebook interface.
In order to get the data you'll need a valid access token, for example if you try the url for the same user's feed you'll get:
{
"error": {
"message": "An access token is required to request this resource.",
"type": "OAuthException",
"code": 104
}
}
You can try this same url with the Graph API Explorer, and after you authorize this app, it will have an access token and you'll get the public feed of that user.
The access token used there (in the API Explorer) is an app access token, which is probably the easiest token to get, here's the doc about Authenticating as an App.
Once you get an access token you can start querying the graph api and get the public data.
You can do it with other access token types, such as a user token.
Edit
Even when things are public for users on facebook, they are not public for applications and you will need to ask for the right permissions for let's say the education (user_education_history).
You can see exactly which permission you need for each field in the documentation of the User object, in the fields table the 3rd column is titled Permissions and there you'll see what you need per field.
Related
I am writing a Razor page application that get's the users data in Azure AD in the form of a HTTP GET call.
However, most of the documentation I found says to use the MS GRAPH library- is there a way to get the access token on behalf of the user with just standard HTTP?
I am not sure how to get the prompt to log into Office365 to show up from my razor pages application.
You could get access token with auth code flow.
Get an authorization code in browser:
https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize?
client_id={client-id}
&response_type=code
&redirect_uri={redirect_uri in azure portal}
&response_mode=query
&scope=https://graph.microsoft.com/.default
&state=12345
Get access token with the previous code:
POST https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token
client_id={client-id}
&scope=https://graph.microsoft.com/.default
&code={code from previous step}
&redirect_uri={redirect_uri in azure portal}
&grant_type=authorization_code
You will obtain the upn(The username of the user) and others after decoding the access token. If you have the required permission of MS Graph, you could also get the details of user with the API.
I want my facebook application to create events in my facebook page. Everyone that use my application will be able to publish events to my facebook page, so I don't want to give admin or contributor to everyone, I want my web app ( appId + secret code) to have the rights to publish
When I'm trying this:
client.AccessToken = "{app-id}|{app-secret}";
dynamic result = client.Post("/{page-id}/events", new{
name = "testEvent",
start_time = "2014-04-11T19:00:00-0700",
end_time = "2014-04-11T20:00:00-0700",
}
);
I received this error
{
"error": {
"type": "Exception",
"message": "You must be an admin of the specified page to perform the requested action.",
"code": 1373019
}
}
Any idea?
You should do the following:
Get a Page Access Token via the /me/accounts endpoint (the logged
in User must be the administrator of this page). You can do this via
https://developers.facebook.com/tools/explorer?method=GET&path=me%2Faccounts Be sure that you have requested the manage_pages, create_event and publish_stream permissions beforehand.
Exchange your short-lived Page Access Token to a non-expiring one as described here:
What are the Steps to getting a Long Lasting Token For Posting To a Facebook Fan Page from a Server
Use the newly generated Page Access Token in your App
The token that you are using is called the App Access Token, and according to the documentation of /{page-id}/events, it says:
An page access token for the page with create_event permission is required.
Now to get the page access token-
First you need to re-authenticate the user (by calling the facebook login/auth again) by adding the permissions:
manage_pages, required for getting the page access token
create_event, required for creating an event
Then make the call:
\GET /{page-id}?fields=access_token, you will get the page access token in response.
Use the token generated in step-2 and make the call you are making, then it will be a success.
(If required, you can also extend this page access token that will never expire- see here)
Learn more about access tokens here.
I've created a web application that uses the OAuth authentication and universal connectors as explained in this tutorial, and started to fiddle around a little to add support for other providers like Yahoo and LinkedIn. So the authentication part works and users are created in the asp.net Membership provider. Also, all the providers return the accesstoken which I supposedly can use to retrieve more information regarding the user.
I'd really like to acquire the profile image, but it seems every provider has a different way of requesting this information. Twitter even describes a way to authorise every request by changing the HTTP header information.
Whilst reading this information on the websites of the various providers I was wondering whether this functionality isn't also already included somewhere in DotNetOpenAuth.AspNet or Microsoft.AspNet.Membership.OpenAuth implementation.
How can I use DotNetOpenAuth.AspNet and/or Microsoft.AspNet.Membership.OpenAuth to request the profile image of the loggedin user using the just acquired accesstoken?
UPDATE in response to Leo's answer
I use the following code to make a call on LinkedIn's API.
string accessToken = extraData["accesstoken"]; // Extra Data received from OAuth containing the accesstoken.
WebRequest request = WebRequest.Create("https://api.linkedin.com/v1/people/~:(id,first-name,last-name,date-of-birth,email-address,picture-url)?oauth2_access_token=" + accessToken);
using (WebResponse response = request.GetResponse())
{
// do something with response here.
}
Error message is "The remote server returned an error: (401) Unauthorized.".
What am I doing wrong?
The answer is simple...you can't use any of these. These are wrappers of OAuth and OAuth only specifies how you can authenticate a user. Now, to request the user's profile photo you will need to use the external provider's own API and you will need most likely a valid access token. So, you will need to use one of these implementations of OAuth to authenticate a user and the recieve an access token, store the access token somewhere (usually a cookie) and then use the access token to make sub-sequent calls to the provider's APIs. Examples and links....
Facebook's Graph API allows you to retrieve users profiles
https://developers.facebook.com/docs/graph-api/quickstart/
notice that all examples in the link above will require you to include the access token in a parameter named access_token, for example
https://graph.facebook.com/me?method=GET&format=json&suppress_http_code=1&access_token={your-access-token}
Google...
https://www.googleapis.com/oauth2/v3/userinfo?access_token={your-access-token}
LinkedIn...
https://api.linkedin.com/v1/people/~:(id,first-name,last-name,date-of-birth,email-address,picture-url)?oauth2_access_token={your-access-token}
You can get more specific information from these providers' websites
Let me know if you have any other doubts I might be able to help you since I have implemented stuff like these before.
Cheers, Leo
I'm trying to get the Facebook C# SDK to work with offline_access and a console application.
From what I understand, I have to:
Ask for auth for offline_access - that's easy.
Get the "code" that is returned by FB when the user authorizes offline_access
Use ExchangeCodeForAccessToken to get a valid access token each time
I can't figure out how to grab the code in (2) though?
Thanks
Getting the "code" and exchanging the code for a token is something you usually do if you are doing authentication in a server-side workflow. It may be a bit of a red herring in your scenario. There are easier ways to just get the access token directly.
It would help to have more context (including code) showing how you are getting offline_access (javascript, server-side OAuth, ...?)
When you get offline_access via the Javascript SDK, which in my opinion provides the best user experience, you will get the access token back in the response from Facebook:
FB.login(function (response) {
if (response.session) {
if (response.perms) {
var accesstoken = response.session.access_token;
// do something with the token...save it, use it, etc.
} else {
// re-prompt for permissions
}
}
}, { perms: 'offline_access' });
Once you have it in javascript you can stuff it into a hidden form field, put it in an ajax post to your server, or whatever.
Note that in my experience you do not actually need the saved user access token to do offline api calls for all api methods. You can just use an app access token in some cases, and Facebook will let it fly if you have offline_access and the other required permissions. They did post a developer blog post in the last week stating you need a token for some api calls where it was not previously required, so that might be changing.
Also be aware that these tokens can go bad. For example if the user changes their Facebook account password it invalidates all access tokens. So it is good to test them, catch OAuth exceptions, and have a way to bring this to the user's attention to re-prompt for permissions and get a new access token.
After the user grants you the "offline_access" permission, Facebook will redirect the user to your application URL with a "code" query string parameter. You need to grab this "code" query string parameter.
Then, make another WebRequest using this "code" value to get the access token. Use the following URL to make the request:
https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}
The WebResponse can be parsed like this:
NameValueCollection qs = HttpUtility.ParseQueryString(response);
token = qs["access_token"];
You need to persist the token in a database for use in your console application.
At present i have created an api_key ,api_secret key and generated an access token after creating an facebook app.i have used these access token
https://graph.facebook.com/search?q=" + txtSearch.Text.Trim() + "&type=user&access_token=" + tokenFB
in my code to fetch data.... The problem i'm facing is I'm not able to view public data of strangers my code fetches all data of my friends but not others in facebook except their name and profile picture . i want to get data of all facebook users that are have public access.. can sum1 help with what should be my approach? Is there no way of a access token that help me fetch data of all users and not just my friends.
All of this depends on the permissions level that the user sets on their profile. In short you will need to adapt to different settings and hence different information for each user.
You can however request more permissions from Facebook for such a purpose.
Refer to Facebook's documentation here.