我是一名 Google Apps 用户,试图在 VB.NET 中编写一个函数来将文件上传到特定用户的 Google Docs 帐户(当然都在我的 Google Apps 域中)。我已经尝试使用用于 ASP.NET 的 Google GData API 进行此操作,但不幸的是,根据 Googler 的说法,该库没有将 xoauth_requestor_id 添加到 url...
我不知道如何添加它,也不知道如何手动构建 HTTP POST 请求,尤其是关于如何发送谷歌在其网站上提到的 XML 有效负载,如下所示......
下面是我尝试使用以下代码手动构建到 Google Docs 的 HTTP POST:Google GData API 页面
此代码返回 401-未经授权的错误:
Protected Sub UploadToGDocs2()
Dim str As String = ""
'The commented-out code below is the HTTP POST code mentioned on
'http://code.google.com/apis/accounts/docs/OAuth.html#tokensGADomains
'
'POST /feeds/documents/private/full?xoauth_requestor_id=j.doe%40example.com HTTP1.1
'Host: docs.google.com()
'Content-Type: application/atom+xml
'Authorization: OAuth()
'oauth_version="1.0",
'oauth_nonce="1c4fbbe4387a685829d5938a3d97988c",
'oauth_timestamp="1227303732",
'oauth_consumer_key="example.com",
'oauth_signature_method="HMAC-SHA1",
' oauth_signature = "lqz%2F%2BfwtusOas8szdYd0lAxC8%3D"
'<atom:entry xmlns:atom="http://www.w3.org/2005/Atom">
' <atom:category scheme="http://schemas.google.com/g/2005#kind"
' term="http://schemas.google.com/docs/2007#document" />
' <atom:title>Company Perks</atom:title>
'</atom:entry
'Here's my attempt to build the HTTP POST command in .NET
Dim web As New System.Net.WebClient()
web.Headers.Add("Host", "docs.google.com")
web.Headers.Add("Content-Type", "application/atom+xml")
web.Headers.Add("Authorization", "OAuth")
Dim keyval As New StringBuilder
keyval.Append("oauth_version=1.0")
keyval.Append("&oauth_nonce=1c4fbbe4387a685829d5938a3d97988c")
keyval.Append("&oauth_timestamp=1227303732")
keyval.Append("&oauth_consumer_key=myDomain.com")
keyval.Append("&oauth_signature_method=HMAC-SHA1")
keyval.Append("&oauth_signature=mySecretKey")
'I have no idea what to do with the XML code below. Where does it go?
keyval.Append("&<atom:entry xmlns:atom='http://www.w3.org/2005/
Atom'>")
keyval.Append("<atom:category scheme='http://
schemas.google.com/g/2005#kind'")
keyval.Append("term='http://schemas.google.com/docs/
2007#document'")
keyval.Append("<atom:title>Company Perks</atom:title>")
keyval.Append("</atom:entry")
Dim d As Byte() =
System.Text.Encoding.ASCII.GetBytes(keyval.ToString)
Dim myUrl As String = "https://docs.google.com/feeds/someone
%40myDomain.com/private/full?xoauth_requestor_id=someone
%40myDomain.com"
Dim res As Byte() = web.UploadData(myUrl.ToString, "POST", d)
Response.Write(System.Text.Encoding.ASCII.GetString(res))
End Sub