在新帐户的 Facebook 响应中反序列化 XML 异常

发布于 2024-09-26 04:58:37 字数 3340 浏览 4 评论 0原文

我正在使用 Facebook-c-sharp 将照片上传到 Facebook。这段代码工作正常,但对于 Facebook 的新帐户,这段代码似乎有问题。我认为这可能与 Facebook 的新隐私政策有关,而且上述代码的作者已经停止开发新版本。 问题似乎出在创建相册上。使用FB的旧帐户,可以正确创建相册并上传照片。对于新帐户(最近创建,大约 20 天前),相册已创建,但当 Facebook 返回 XML 响应时,当我尝试反序列化 XML 时,会抛出 XMLException。

旧帐户的 XML 响应如下

    <?xml version="1.0" encoding="UTF-8"?>
<photos_createAlbum_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd">
  <aid>2470234845268380151</aid>
  <owner>number</owner>
  <name>AlbumName</name>
  <created>number</created>
  <modified>number</modified>
  <description>description</description>
  <location>location</location>
  <link>url of the album created</link>
  <size>0</size>
  <visible>privacy setting</visible>
  <modified_major>number</modified_major>
  <object_id>number</object_id>
</photos_createAlbum_response>

新帐户的 xml 响应如下

    <?xml version="1.0" encoding="UTF-8"?>
<photos_createAlbum_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd">
  <aid>100001291780378_24994</aid>
  <owner>number</owner>
  <name>AlbumName</name>
  <created>number</created>
  <modified>number</modified>
  <description>description</description>
  <location>location</location>
  <link>url of the album created</link>
  <size>0</size>
  <visible>privacy setting</visible>
  <modified_major>number</modified_major>
  <object_id>number</object_id>
</photos_createAlbum_response>

我收到的 XMLException 如下(再次说明:这只发生在新帐户中。旧帐户反序列化工作得很好)

type=Mono.Facebook.XMLException

message=Failed parsing XML

stack=
   at Mono.Facebook.Util.GetResponse[T](String method_name, FacebookParam[] parameters)
   at Mono.Facebook.FacebookSession.CreateAlbum(String name, String description, String location)
   at Facebook.PostToFacebook(List`1 photos, String apiKey, String apiSecret, String authToken)



type=System.InvalidOperationException

message=There is an error in XML document (0, 0).

stack=
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
   at Mono.Facebook.Util.GetResponse[T](String method_name, FacebookParam[] parameters)

正如您可以看到来自的两个 XML 响应新旧帐户的 Facebook 类似。但对于新帐户反序列化仍然失败。为什么会发生这种情况?我在这两个 xml 文件中发现的唯一区别是第二个 XML 文件的 (albumid) 中存在“下划线 _”。但据我所知,下划线是有效的 xml,解析应该不会有问题,对吗?有人能告诉我为什么会收到此错误吗?

编辑:

我成功了。如上所述,下划线导致了问题。我没有检查这一点,这对我来说是很愚蠢的。在 C# 代码中,aid 被声明为 long。由于带下划线的字符串不能反序列化太长,所以我遇到了异常。我将 Facebook 的 xml 响应复制到一个 xml 文件中,然后执行 xsd Facebook.xml 和 xsd Facebook.xsd 来获取 Facebook.cs。我将生成的 cs 文件与我拥有的文件进行了比较,发现 aid 在生成的 cs 中声明为 String,在我的文件中声明为 long。

I am using the Facebook-c-sharp to upload photos to Facebook. This code works fine but for new accounts of Facebook this code seems to have problem. I thought may be its something to do with the new privacy policies of Facebook and moreover authors of above code have stopped working on new releases.
The problem seems to be with creating albums. With old account of FB, the album gets created and photos are uploaded properly. For new account(created recently, about 20 days back), the photo album gets created but when the Facebook returns the XML response, I get an XMLException thrown when I try to deserialize the XML.

The XML response for old account is below

    <?xml version="1.0" encoding="UTF-8"?>
<photos_createAlbum_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd">
  <aid>2470234845268380151</aid>
  <owner>number</owner>
  <name>AlbumName</name>
  <created>number</created>
  <modified>number</modified>
  <description>description</description>
  <location>location</location>
  <link>url of the album created</link>
  <size>0</size>
  <visible>privacy setting</visible>
  <modified_major>number</modified_major>
  <object_id>number</object_id>
</photos_createAlbum_response>

The xml response for new account is below

    <?xml version="1.0" encoding="UTF-8"?>
<photos_createAlbum_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd">
  <aid>100001291780378_24994</aid>
  <owner>number</owner>
  <name>AlbumName</name>
  <created>number</created>
  <modified>number</modified>
  <description>description</description>
  <location>location</location>
  <link>url of the album created</link>
  <size>0</size>
  <visible>privacy setting</visible>
  <modified_major>number</modified_major>
  <object_id>number</object_id>
</photos_createAlbum_response>

The XMLException I am getting is below (again:this is happening only for new accounts. old account deserialize works just fine)

type=Mono.Facebook.XMLException

message=Failed parsing XML

stack=
   at Mono.Facebook.Util.GetResponse[T](String method_name, FacebookParam[] parameters)
   at Mono.Facebook.FacebookSession.CreateAlbum(String name, String description, String location)
   at Facebook.PostToFacebook(List`1 photos, String apiKey, String apiSecret, String authToken)



type=System.InvalidOperationException

message=There is an error in XML document (0, 0).

stack=
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
   at Mono.Facebook.Util.GetResponse[T](String method_name, FacebookParam[] parameters)

As you can see both the XML responses from Facebook for old and new account are similar. But still for new account deserialize fails. Why is this happening? The only difference I could find in both the xml files was the presence of "underscore _" in (albumid) in second XML file. But as far as I know underscore is valid xml and should not be a problem parsing right? Can someone tell why am I getting this error.

EDIT:

I got it working. As mentioned above, underscore was causing the problem. It was quite silly on my part that I did not check that. In C# code aid was declared as long. Since string with underscore cannot be deserialized to long I was getting exception. I copied the xml response from Facebook into a xml file then did xsd Facebook.xml and xsd Facebook.xsd to get Facebook.cs. I compared generated cs file with the one I have and saw that aid was declared as String in generated cs and long in my file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青巷忧颜 2024-10-03 04:58:37

虽然这确实不能直接解决你的问题,但我认为这里更大的问题是你使用的 Facebook 库非常旧且没有维护。我强烈建议切换到支持 OAuth、Graph API 和 Rest API 的新库。这是我创建并推荐的:http://facebooksdk.codeplex.com

While this really doesn't address your question directly, I think the bigger issue here is that you are using a Facebook library that is very old and not maintained. I would highly suggestion switch to a new library that supports OAuth, the Graph API, and the Rest API. Here is the one that I created and would recommend: http://facebooksdk.codeplex.com

倾城°AllureLove 2024-10-03 04:58:37

我成功了。如上所述,下划线导致了问题。我没有检查这一点,这对我来说是很愚蠢的。在 C# 代码中,aid 被声明为 long。由于带下划线的字符串不能反序列化太长,所以我遇到了异常。我将 Facebook 的 xml 响应复制到一个 xml 文件中,然后执行 xsd Facebook.xml 和 xsd Facebook.xsd 来获取 Facebook.cs。我将生成的 cs 文件与我拥有的文件进行了比较,发现 aid 在生成的 cs 中声明为 String,在我的文件中声明为 long。

I got it working. As mentioned above, underscore was causing the problem. It was quite silly on my part that I did not check that. In C# code aid was declared as long. Since string with underscore cannot be deserialized to long I was getting exception. I copied the xml response from Facebook into a xml file then did xsd Facebook.xml and xsd Facebook.xsd to get Facebook.cs. I compared generated cs file with the one I have and saw that aid was declared as String in generated cs and long in my file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文