I have developed an application to download user names and Ids from face book graph API. But I cannot get their public information like birthdays.
So my approach is to download HTML of about pages and read Birthday from that.
my code follows; I developed my application in MVC4:
public ActionResult ExternalLoginCallback(string returnUrl)
{
AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
if (!result.IsSuccessful)
{
return RedirectToAction("ExternalLoginFailure");
}
if (result.ExtraData.Keys.Contains("accesstoken"))
{
Session["facebooktoken1"] = result.ExtraData["accesstoken"];
string fbToken = result.ExtraData["accesstoken"];
ViewBag.TokenToview = fbToken;
ViewBag.Id = 5;
WebRequest myWebRequest = WebRequest.Create("https://www.facebook.com/amila.u.abeyrathne/about");
WebResponse myWebResponse = myWebRequest.GetResponse();
Stream ReceiveStream = myWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(ReceiveStream, encode);
string strResponse = readStream.ReadToEnd();
}
When I use this I only get html of LOG IN page of facebook. How can I use my facebook login session in this application?
To get information like this, you'd need to hit Facebook Graph API endpoints like mentioned here
. You'd need an access token gneerated with the necessary permissions as specified here in order to submit a working request. I'm assuming the access token you're using in this code is generated by querying OAuth in the specified manner.
It is not possible to get those information from facebook graph Search.The solution is to develop application that can log in to facebook and scrap the HTML of the facebook wall of each user whom userids have being downloaded. After scraping the HTML read the text from htlm is the solution. But it is not that much accurate. Birthdays and other information in the about page is not possible to read on that way either because there is no way to navigate to the about page in that way.
Ur application session data cannot be read by facebook API, so need to send the token in subsequent requests.
Getting FB Page data from facebook using C#
I think your approach is wrong. when you log in to a website it is created session cookie in client browser. Then it append to each request header when send request to that web site again. session cookie created on client browser not the server.
What you are doing is you send request to facebook from your server not from the client. Then there is no valid session cookie append to your request.
If you what to go this way you have to read the facebook page using javascript. But it is not the recommended way.
You need to crate facebook app for your website.
Then need to Select permissions to request from users. (for your case use user_birthday )
Then authenticate your website using that app. Then you can get what ever the details that you want using graph api if users gave permission.
Related
I am writing an app that will talk with Salesforce. Salesforce provides access to APIs via OAuth. I've been attempting to go through the OAuth authentication process described here. Currently, I'm attempting to authorize my app. I have the following code.
// Ask Salesforce for a request token
var request = (HttpWebRequest)(WebRequest.Create(String.Format("https://login.salesforce.com/services/oauth2/authorize?response_type=code&client_id={0}&redirect_uri=http://localhost:5004/home/AuthCallback", CONSUMER_KEY)));
request.Method = "POST";
request.ContentType = "application/json";
// Retrieve the request token from the response
var response = (HttpWebResponse)(request.GetResponse());
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string accessCodeData = accessCodeReader.ReadToEnd();
}
This code is triggered when a user clicks a button in my ASP.NET MVC view. When executed, this code calls to Salesforce. I see a request in fiddler. The request header looks like this:
POST /services/oauth2/authorize?response_type=code&client_id={consumerKey}&redirect_uri=http://localhost:5004/home/AuthCallback HTTP/1.1
I am in fact passing my consumer key, I'm just removing it from the example. Regardless, this request returns a 302, with a body size of 0. I might be misunderstanding something. However, I was expecting to get a request token. I was then going to use the request token to get the access token.
What am I doing wrong?
You are misusing the API.
Take a closer look at the sequence diagram at their page (under Obtaining an Access Token): in the auhorization_code flow you are supposed to redirect the browser to their page so that the user sees the login page, provides his/her credentials and you get the token back. Instead, you are trying to POST there using a web request from your server.
This particular flow belongs then to the passive flows group, this group is intended to be used in browser apps, your server redirects the browser to their server and you basically get the response to the uri passed in the redirect_uri parameter and this should point back to your application at your server.
There are other flows, of them one is suited for non-browser apps, it is called resource owner password flow. In this flow it is your application that hosts the login UI and you send the username/password to the authorization server and you get the token back. It is to be read in their docs however whether this flow is supported.
Read more here: http://aaronparecki.com/articles/2012/07/29/1/oauth2-simplified
Take a look how I handle the OAuth2 flow to Google, using the DotNetOpenAuth library. This is a direct solution, applying it to any other provider should be straightforward.
http://www.wiktorzychla.com/2014/11/simple-oauth2-federated-authentication.html
im just going to dive straight in and give you a little background on what im trying to do, what i've tried, and the obstacles in my way. so here goes..
MY GOAL
To post to a facebook profile or wall from a desktop application.
The desktop application will be used by individual users who have their own facebook account.
The aim is to create each user their own facebook app and get the app id and app secret.
And then use the app id and app secret in the desktop application ( Saved somewhere in database or config )
to allow the user of the desktop application to post to their profile without having to enter their email address and password to login first
So in summary, 1 facebook app per facebook account.
The app settings will be saved in each users desktop application
Post to their own facebook via their own facebook app without logging in
The main emphasis here being that the user does not have to log in manually via a window or browser.
I have created my facebook app and have set it to live status.. i have also added in all possible permissions and extended permissions in the settings ( Within facebook ) just to make sure i wasnt missing anything.
So I do have my AppID and App secret.. I also have my Client Token which it says to use in place of the app secret for 'auth methods'. I use this to get my access token. I tried the app secret and it doesnt return the access token
MY ATTEMPTS :
C# FACEBOOK SDK
So, i started with and am still trying to use the c# sdk
I can retrieve my access token but cannot post.
I get he below errors all the time with whatever i try... these are just 2 of the many code examples i have tried.
(OAuthException - #2500) An active access token must be used to query information about the current user.
dynamic r = fb.Post("me/feed", new { message = "My seconds wall post using Facebook C# SDK" });
(OAuthException - #190) The client token cannot be used for this API
dynamic r = fb.Post("kevin.maguire.965/feed", new { message = "My second wall post using Facebook C# SDK" });
I read the following extract from the below link which states my access token is an app token and i need a access token for a user or page?
Need Help on OAuthException Code 2500
Error 2500 means you have no access token or an app access token but are trying to access /me/ - 'me' is a placeholder for 'current user or page ID' so won't be valid without an access token for a user or page
So, i have tried to get the userID back using the following Answer ( Facebook C# SDK Get Current User )
var fb = new FacebookClient("access_token");
dynamic result = fb.Get("me", new [] { fields = "id" });
var userId = result.id;
I get the access token which i assume is the app token and not the user token
dynamic result = fb.Get("oauth/access_token", new
{
client_id = this.ApplicationId,
client_secret = this.AppSecret,
grant_type = "client_credentials"
});
fb.AccessToken = result.access_token;
So i have no idea at this moment in time how to post to my profile
I am able to achieve the above using twitter where i can use the secret, token, id etc ... they provide and I can successfully post to my twitter account from a desktop application WITHOUT logging into my twitter account.
Another user has also found it quite easy to post to twitter without any real issues. ( facebook c# sdk getting started )
He also seems to have had success which i have not using the same code - this code was uses in June 2012 so there could have been breaking changes released since
then.
I got the message : (OAuthException - #2500) An active access token must be used to query information about the current user... when i used the sdk.
When i tried to get the access token using a web request and then pass that token to the sdk object to create a facebookclient i got this message
(OAuthException - #190) Invalid OAuth access token signature.
WebRequest request = WebRequest.Create("https://graph.facebook.com/oauth/access_token? grant_type=client_credentials&client_id=999999999999999&client_secret=edefefr8e09r8e08r080r8er0e");
request.Method = "POST";
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string accesstoken = reader.ReadToEnd();
MessageBox.Show("accesstoken")
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
var fb = new FacebookClient(accesstoken);
dynamic parameters = new ExpandoObject();
parameters.message = "test";
dynamic result = fb.Post("me/feed", parameters);
var id = result.id;
Obviously in the code above i changed the id and secret to dummy values.
So basically folks...the above links and above code are only a pinch of what I have tried to date in the last few days... i've basically ran out of options and am missing something here which could be averting my attention easily.. or maybe not :) i dont know.
If any one would even have a simple windows form, or wpf window application example using the c# sdk or using restsharp or just using WebRequest object with 'POST' method then I would be eternally greatful.
Just to iterate again that it is a desktop application and not ASP.net .
Many thanks folks for your attention and time.
From what I can see you are supplying an incorrect access token. Since you haven't provided the code with regards to obtaining the access token, may I suggest you take a look at this link which explains how to build a Facebook application, including obtaining an access token via a WebBrowser control.
EDIT: You are supplying the app access token but are trying to post as a user. For this operation you need a user access token which you can obtain by following the steps in the link above.
I have a webpage which is windows forms authenticated ,and i want to download a copy of this page's HTML in to my server, when user request this page. I have tried something like this
using (WebClient client = new WebClient())
{
string htmlCode = client.DownloadString("http://aksphases:200/lynliste.aspx");
}
which doesn't gives me correct result because of the URL I had passes to system creates new session.And in that case i need to authenticate this web-request,which I can't do.Only way to authenticate this webpage is that user log in manually(I know ways to authenticate werequests by code,but I can't try that here for some special reasons). Is there any other way for me to download current page's HTML which is running in in browser with out authenticating the URL.
You could send the current forms authentication cookie along with the request:
using (WebClient client = new WebClient())
{
client.Headers[HttpRequestHeader.Cookie] =
System.Web.HttpContext.Current.Request.Headers["Cookie"];
string htmlCode = client.DownloadString("http://aksphases:200/lynliste.aspx");
}
This way we are basically transferring the current HTTP request cookies to the remote HTTP call.
If the web server does not allow anonymous access then there is no way around - you must authenticate yourself with the web site.
However, contrary to your belief that log on operation has to be done manually, it can be done via code also. In case of windows authentication, pass credentials via Credentials property. For Forms authentication, you need to POST log-on credentials to login page and then use the authentication cookie from the response in subsequent request (Use tool such fiddler to inspect request/responses from browser to replicate same within your code).
Similar to the CSV file which can be downloaded from http://download.finance.yahoo.com/d/quotes.csv?s=RHT . How can I downloada file which requires authentication?
I can simply use
My.Computer.Network.DownloadFile("http://download.finance.yahoo.com/d/quotes.csv?s=RHT,MSFT,NOVL&f=sl1c1d1&e=.csv", "h:\s.csv")
To download the file which is available in public. I tried setting the username and password as per the MSDN documentation but all I get is the HTML content of the login page.
If the site uses Cookie based authentication, you'll need to post the login details to the server, collect the cookies, then pass them up on the request for the file.
That's not as easy as it sounds...
There's an example here
http://blogs.msdn.com/dgorti/archive/2005/08/16/452347.aspx
Phil
It all depends on whether or not the web site will interpret your username/password when you provide it in your HTTP request. Some sites/protocols will, and others won't.
Here's an article that shows how you can download with the WebRequest class.
Downloading Files with the WebRequest and WebResponse Classes
All you need in addition to this article is adding your username/password to the WebRequest. You can do it like this:
// Create a request for the specified remote file name
WebRequest request = WebRequest.Create(remoteFilename);
if (request != null)
{
string username = "your username";
string password = "your password";
request.Credentials = new System.Net.NetworkCredential(username, password);
...
}
Unfortunately, if using this method doesn't work, then Yahoo's finance page doesn't allow providing a username/password automatically, and you'd have to login "the old fashioned way" to be able to download your file.
It sounds like the site you are working with is using forms authentication. Unless you have access to a version that uses realm authentication you will probably need to use HttpWebRequest and fake POST request to the site while you have a CookieContainer so you can retain the token. Then you would be able to include that token in a get request to download the CSV file.
I'm trying to login directly to Google Analytics. To explain, I have an account system and I'd like when you select an ASP.NET button for instance it re-directs you - via a silent login - to a specified Google Analytics account.
I've looked long and hard at Dave Cullen's ASP.NET library and although I can login 'silently' using HttpWebRequest, I can't then stick the user on that page. I'm having allsorts of dramas with a 'Cannot send a content-body with this verb-type' error too.
Here is the very basic code I have currently based on Dave's library;
string token = GoogleAnalytics.getSessionTokenClientLogin(username, password);
NameValueCollection profiles = GoogleAnalytics.getAccountInfo(token, GoogleAnalytics.mode.ClientLogin);
HttpWebRequest theRequest = (HttpWebRequest)WebRequest.Create("https://www.google.com/analytics/settings/?et=reset&hl=en_uk&et=reset&hl=en-US&et=reset&hl=en-GB");
theRequest.Headers.Add("Authorization: GoogleLogin auth=" + token);
Stream responseBody = theRequest.GetRequestStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(responseBody, encode);
My question therefore is; 1. can this be done? and 2. is this even the right way to do this?
Advice welcomed!
I'm not sure what the overall goal of signing someone in to Google Analytics automatically is, but if its just to display some of the data that is in Google Analytics you might want to consider using the Google Data API to pull the information that you want from Google Analytics. You could create a simple dashboard of what they really need to see without giving access to other things in Google Analytics, by logging them in you are most likely giving them access to data and tools that they just don't need?
Check out the API if it doesn't fit your needs maybe provide some more information on the overall goal is of this functionality.
http://code.google.com/apis/analytics/
Unless you're willing to implement a proxy server to proxy google analytics, I don't think you're going to be able to do this because you can't assign cookies to the client for another domain.
If the auth tokens are stored in cookies you can add the cookies to your ASP.NET response - then host the google page in an IFRAME just by setting the src (no inlining). That IFRAME will "inherit" the cookies from your parent page and the page will think it's authenticated.