2-legged oAuth上传到Google文档失败

时间:2011-03-09 14:57:31

标签: asp.net vb.net oauth google-data-api

我是Google Apps用户,尝试在VB.NET中编写功能,将文件上传到特定用户的Google文档帐户(当然,这些都在我的Google Apps域中)。我尝试使用Google GData API for ASP.NET,但遗憾的是,根据Google员工的说法,此库不会在网址中添加xoauth_requestor_id ...

我不知道如何添加它,但不知道如何手动构建HTTP POST请求,尤其是关于如何发送Google在其网站上提到的XML有效负载如下所示......

以下是我尝试使用以下代码手动构建针对Google文档的HTTP POST:Google GData API page

此代码返回401-Unauthorized错误:

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

0 个答案:

没有答案