我试图借助Facebook C#SDK ver将照片上传到脸谱相册。 5.4.1.0,但继续失败并出现以下错误:“OAuthException)(#200)应用程序无法进行此API调用。”
我想要实现的目标是:
到目前为止,我设法为该页面获取Access Token,并在该页面内创建一个专辑
代码是:
Protected Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click
Dim fb As New Facebook.FacebookClient("MyAccessToken")
Dim dic As IDictionary(Of String, Object) = DirectCast(fb.Get("/me/accounts"), IDictionary(Of String, Object)) 'List of all my pages
Dim dicy As IList(Of Object) = DirectCast(dic("data"), IList(Of Object)) 'cast the Data part of JSON to list
Dim page As IDictionary(Of String, Object) 'Individual item inside the dicy :)
For i As Integer = 0 To dicy.Count - 1
page = DirectCast(dicy(i), IDictionary(Of String, Object))
If page("id") = PageID Then 'try to match given PageID with the one in the list
fb.AccessToken = page("access_token") 'change access token to the one of the page
Exit For
End If
Next
'Create an album
Dim fbparams As New Dictionary(Of String, Object)
fbparams.Add("message", "New album")
fbparams.Add("name", "my album name")
'post to Facebook and get the AlbumID
Dim Album As Facebook.JsonObject = fb.Post("/me/albums", fbparams)
'try to upload photos
Call UploadPhotoToAlbum(fb, Album(0).ToString, Server.MapPath("~/myimage.jpg"))
End Sub
上传文件的代码是:
Private Sub UploadPhotoToAlbum(ByVal fbApp As Facebook.FacebookClient, ByVal AlbumID As String, ByVal ImagePath As String)
Dim media As New Facebook.FacebookMediaObject
media.ContentType = "image/jpeg"
media.FileName = ImagePath
Dim filebytes As Byte() = System.IO.File.ReadAllBytes(ImagePath)
media.SetValue(filebytes)
Dim upload As New Dictionary(Of String, Object)
upload.Add("name", "Picture No 1")
upload.Add("message", "This is my first uploaded image")
upload.Add("@file.jpg", media)
fbApp.Post("/" & AlbumID, upload)
End Sub
我的应用具有以下权限:create_note manage_pages offline_access photo_upload publish_stream read_stream share_item status_update user_photo_video_tags user_photos user_videos video_upload