Obtaining Photos and Albums from Facebook using C# - c#

I'm trying to parse through and obtain my (my personal account not my app) albums from Facebook using the Facebook C# SDK. My goal is to grab the 10-12 most recent photos on my account. However, I understand I have to grab the albums first.
So, I've tried numerous things and ended up with the following url which returns a 400 Bad Request:
https://graph.facebook.com/{my_user_id}/albums?access_token={my_access_token}
The token was obtained by calling:
https://graph.facebook.com/oauth/access_token?client_id={0}&client_secret={1}&grant_type={2}&scope={3}
Any ideas why I'd be getting the 400?

When using grant_type = client_credentials you're requesting the an app access token. This will allow you to do various administrative actions for your application. See App Login in http://developers.facebook.com/docs/authentication/.
However, when using the user-parts of the Graph Api you need to perform a User Login using the oAuth Dialog. There are different ways of doing this such as with the Javascript SDK which should be straightforward to use.
I've not found a nice way of doing this in a standalone web app using Facebook C# SDK without the Javascript SDK (it's easy in a canvas app using the CanvasAuthorize attribute).
Here's an example of how to do it i a WinForms app http://blog.prabir.me/post/Facebook-CSharp-SDK-Writing-your-first-Facebook-Application.aspx. It might work in a Asp.Net app if you could use the WebBrowser control. I've tried with WebClient but didn't have any luck.
Update
By looking at the sample here http://facebooksdk.codeplex.com/SourceControl/changeset/view/534da45e108f#Samples%2fCSMvcWebsite%2fControllers%2fHomeController.cs it looks like you should be able to use the FacebookAuthorize attribute in a standalone site.

Error code 400 means that the request was not correctly formatted. Verify that that the Final URL looks OK and try it in a browser.
The request could not be understood by
the server due to malformed syntax.
The client SHOULD NOT repeat the
request without modifications.
I.e. that you try the following https://graph.facebook.com/someuser/albums?access_token=1234 You would pre presented with the following:
{
"error": {
"type": "OAuthException",
"message": "Invalid OAuth access token."
}
}
If you provide an OK token and a real user, the result will probably look a bit different, but in your case you get a 400 because there is something wrong with the request.

Related

What are the ways to post a tweet to twitter with asp.net c#? [duplicate]

With Twitter's new OAuth interface, their API is now many times more complex than what it was. And I haven't even looked at Facebook's API yet.
What I'm wondering if there is a method that employs some higher-level, existing code or interfaces to make this a simpler task.
All I want to be able to do is initiate a Twitter tweet or Facebook share on the user's behalf and be able to control the initial text of those messages, from an ASP.NET application.
I found some similar questions on SO, but they had no answers.
EDIT: I know there are things like AddThis and ShareThis, but I need something that will give me control over the default message. It must contain a link with a code that is specific to the current user.
Twitter Integration...
For making Tweets from an ASP.NET application on users' behalf, check out Twitterizer. It's a free, open-source project for integrating with Twitter from .NET applications.
I agree that using OAuth can be a bit daunting, but the Twitterizer API wraps up most of the complexity. I've written an article on using Twitterizer in an ASP.NET application that you may be interested in: Integrating Twitter Into An ASP.NET Website Using OAuth. After reading the article, download the code sample at the end, which is a working demo showing how to use Twitterizer to post a tweet from an ASP.NET website.
Facebook Integration...
For integrating with Facebook, chcek out the Facebook Developer Toolkit. Like Twitterizer, it's an open-source, free API and should get you going in the right direction.
Happy Programming!
After looking around for a while, I found sharethis.com. They have various share buttons you can add to your site that send Twitter tweets, Facebook shares, etc.
It looks like it also supports options to control the URL, so I could modify this to include whatever URL I need.
I haven't yet figured out if I can control the default message text. I'm looking into that.
But it seems like this is probably the simplest way to accomplish what I want.
This is probably what you're after for twitter: https://twitter.com/about/resources/buttons#tweet
Let's you configure a button (or URL to redirect to) that starts the user off with some default text. The user can change the text before they post.
Don't know about facebook.
Twitter Integration:
Check this code and link/article simple and easy :
protected void btnTweet_Click(object sender, EventArgs e)
{
string oauthAccessToken = Session["twtoken"].ToString();
string oauthAccessTokenSecret = Session["twsecret"].ToString();
OAuthHelper oauthhelper = new OAuthHelper();
oauthhelper.TweetOnBehalfOf(oauthAccessToken, oauthAccessTokenSecret, txtTweet.Text);
if (string.IsNullOrEmpty(oauthhelper.oauth_error))
Response.Write("Twit Posted Successfully");
else
Response.Write(oauthhelper.oauth_error);
}
Read more how to get access token and secret key and download OAuthHelper and OAuthUtility Class below is the link -
How to post tweet on behalf of an user from asp.net using oauth authentication
Login with twitter using oauth authentication in asp.net and get access token, screen name and userid

Is there a naming standard for REDIRECT URIS OAuth 2.0?

I just want to know if there is a naming standard on REDIRECT URIS for :
Twitter, LinkedIn, Microsoft, Facebook and Google when using OAuth 2.0?
Because, if I write my domain like that : http://domain.com/account/external-signin.aspx every external login stop working except Twitter and Facebook. The name account/external-signin.aspx is the real URL I'm working with and that I'm supposed to give to every external login.
So, Microsoft give this error:
We're unable to complete your request
Microsoft account is experiencing technical problems. Please try again later.
LinkedIn:
Invalid redirect_uri. This value must match a URL registered with the API Key.
And Google
400. That’s an error. Error: redirect_uri_mismatch
If I remove the page extension .aspx it seems to work although I deliberately write a wrong url like http://domain.com/sign-google, http://domain.com/sign-microsoft etc...
I'm working with MVC5 and C#.
I think I missed a few things ...
Thanks for your help
So finally, here is the correct answer:
It's not you that choose the redirect URL. You must write your domain.com/signin-{suppliername} in your app management.
Example:
Microsoft : https://domain.com/signin-microsoft
LinkedIn : https://domain.com/signin-linkedin
Google : https://domain.com/signin-google
Facebook and Twitter can work with your own redirect URL. Once successfully registered, the effect is immediate. Hope this can help somebody.
Karine
This error is denoting that you're having a miss match with the URL you're returning, and the return URL registered at the API Server. When you register your application, at the server, (for Google: https://code.google.com/apis/console) you have to make sure that the URLs being used would be matching.
After this, you will not get this error, I think on the server you've set this property to, http://domain.com/account/external-signin (without aspx; as you've said that this works without the extension but not with it). So try to change it on the server too.
For Linkedin append your url with "signin-linkedin".e.g. if your url is http://localhost:{portnumber}, make sure its is register in linked in as "http://localhost:{portnumber}/signin-linkedin" and this will do the trick.
Happy Codding :)

Getting a "Unexpected character '<' at line 1, column 0." while trying to connect to Google using GoogleApis library

I'm using Google Apis, instead of Google Sign In, to connect to Google on my app because I'm developping with Xamarin.
This is the library I'm using : https://github.com/xamarin/google-apis
When I'm logging in, i get this error :
Authentication Error
Unexpected character '<'. At line 1, column 0.
Maybe it's because my AccessTokenUrl is not good, but I've tried many things. I know for a fact that my ClientId, my RedirectUrl and my Secret are okay.
When logging in, Google asks correctly for the good permissions that I want, but after I accept, this is when I receive the error.
I've tried finding the request to see if there was the '<' in it but had no luck accessing it.
Is there a good way to connect to Google with Xamarin using this library or I'm just doing something bad?
Should I just do it nativaly on iOS and Android?
Thanks
I just encountered a similar problem using Xamarin.Auth to hit a custom OAuth service (i.e. not Google). In my case, the accessTokenUrl pointed to an action on a controller that was entirely protected by the [Authorize] attribute. Naturally, the user was required to login before hitting the /oauth/authorize endpoint using a web browser, but the request to /oauth/token to exchange the resulting authorization code for an access token was not inside the same web browser/session. It was trying to get back token data in JSON format but was being redirected to an HTML login screen. Once I changed the token endpoint for anonymous access, things started working (Note: A valid authorization code cannot be obtained without authenticating).
General Recommendations
The error message strongly suggests that the response coming back is HTML (or at least some form of XML). This could be an authentication redirect as it was in my scenario, or possibly some sort of error page. I would first start by setting up a proxy. I used Charles Proxy to uncover some interesting information. You will need to configure SSL on the proxy to see anything except high level information. This will show the exact requests coming out of your app to the OAuth application.
Another technique I used was simulating the requests that the OAuth2Authenticator would be making in a web browser and/or Postman. The first request would be to authorize:
https://your.domain.here/oauth/authorize?client_id=<some_client_id>&redirect_uri=https%3A%2F%2Fyour.domain.here%2F&response_type=code&scope=<some_scope>&state=<some_state>
That endpoint should be protected, so you will likely be redirected to something like this:
https://your.domain.here/Account/Login?returnurl=%2Foauth%2Fauthorize%3Fclient_id%3D<some_client_id>%26redirect_uri%3Dhttp%253A%252F%252Fyour.domain.here%252F%26response_type%3Dcode%26scope%3D<some_scope>%26state%3D<some_state>
After authenticating, the authorize endpoint should redirect to your redirect URI with the authorization code and state included as query string parameters. You will use the code in the next step.
Lastly, using a fresh web browser (i.e. new session), you should hit the token endpoint with your new authorization code and other client information.
https://your.domain.here/oauth/token?client_id=<some_client_id>&client_secret=<some_secret>&grant_type=<your_grant_type>&code=<your_authorization_code>&redirect_uri=https%3A%2F%2Fyour.domain.here%2F
If the response is not JSON data, it should give you an indication about what is failing with Xamarin.
Got the same error.
Solved by using https://accounts.google.com/o/oauth2/token as AccessTokenUrl

Xbox Music RESTful API on Azure access problems

The simple goal here is to access the Xbox Music RESTful API on Azure and I stumble on the first part of it which is getting the Access Token. By following this procedure: http://msdn.microsoft.com/en-us/library/dn546686.aspx it's simply not allowing me to get a token.
Thus far I have:
I have a datamarket registration
I registered an application, got a client_id and client_secret
Subscribed to the Xbox Music RESTful API
Then I simply try to get a token either through a POST using XMLHttpRequest or Advanced Rest Client for Chrome and thus far all I receive is this Json response:
{
"error":"invalid_client",
"error_description":"ACS50012: Authentication failed.\r\nTrace ID: d2469189-d620-4725-98c6-544e3899d711\r\nCorrelation ID: 3726a6c7-de19-4873-a90c-b51c7ca447a7\r\nTimestamp: 2014-05-08 14:18:31Z"
}
I did some research for this error here: http://msdn.microsoft.com/en-us/library/gg429787.aspx but can't seem to pull an explanation for this.
The data explorer is also broken as I receive this:
The request resulted in a backend time out or backend error. The team is investigating the issue. We are sorry for the inconvenience. (502)
In the end I assume something is bogus with my account but can't figure it out. Any help with this would be greatly appreciated.
See comment, recreate a new application and copy client secret properly.

Update Status Facebook C# SDK Question

I am trying to write some simple functional code that will be added to an existing large scale project. I used the TweetSharp sdk to get and Access Token & SecretToken, and the stored them, and I can them use those with my API key and secret to post to twitter with no issue. I now need to do the same thing but with a facebook status update. (Someone correct me if I'm wrong), but Facebook doesn't user an Access Token & secret Token, but instead they just use an Access Token. I've written the authorization part of the app that launch the browser, user logs in, click Allow, and then I obtain the Access Token from listening to the URL. I have looked at the documentation for three different SDK/API's and don't see anything specifically related to.. This is how you post a status using the user's Access Token. I have tried a number of API documented methods from the Facebook C# SDK, but they all seem way to complicated compared to how I got this done with Twitter... maybe Facebook just made this more complicated, or maybe the SDK just doesn't offer the functionality..
Can someone point me to an example for Facebook API code that functions like this after getting the AccessToken(The following is a twitter example)
Public Class Form1
Private Sub TweetButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TweetButton.Click
'create new Twitter API object
Dim twitter As New TwitterVB2.TwitterAPI
twitter.AuthenticateWith("ConsumerKey As String", "ConsumerKeySecret As String", "Token As String", "TokenSecret As String")
'post to twitter
twitter.Update(PostTextBox.Text)
'verify post went through
MsgBox("Tweet Posted Successfully")
End Sub
End Class
#
or maybe the workflow for using Tokens in completely different with FB... any suggestions or help in finding an example of how to get this done would be greatly appreciated.
Thanks!
Anyone looking for mostly simple functionality in working with Facebook should simply use the built api's to the facebook framework... ie the rest.api or graph.api ... in it's most basic form, you are just shooting http requests at the facebook server with Access Tokens, UID's and other syntax embedded in the url.. I actually find using this easier than the SDK's.
No response needed anymore on this one... Thank you!

Categories

Resources