Obtaining Photos and Albums from Facebook using C#

寵の児 提交于 2020-01-17 04:50:07

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/5178238/obtaining-photos-and-albums-from-facebook-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!