1

一段时间以来,我们一直在使用现已弃用的 Fusion Tables SQL API 来更新我们的 Google Fusion Table。我们创建了一个 VB.NET 应用程序来验证请求并执行必要的任务并且没有任何问题。我们正在尝试迁移到新的 v1.0 API,但遇到了多个问题。

我能够检索授权令牌,但我的问题在于创建一个 HTTP 请求以成功验证并发布请求。在此示例中,我正在尝试创建一个新表:

    Dim auth As OAuth2Authenticator(Of NativeApplicationClient) = GetAuthorization()
    Dim sURL As New StringBuilder
    Dim sRequest As New StringBuilder
    Dim encoding As New ASCIIEncoding()
    Dim data As Byte() = Nothing

    sURL.Append("https://www.googleapis.com/fusiontables/v1/tables")

    sRequest.Append("{")
    sRequest.Append(" ""name"": ""Insects"",")
    sRequest.Append(" ""columns"": [")
    sRequest.Append("  {")
    sRequest.Append("   ""name"": ""Species"",")
    sRequest.Append("   ""type"": ""STRING""")
    sRequest.Append("  },")
    sRequest.Append("  {")
    sRequest.Append("   ""name"": ""Elevation"",")
    sRequest.Append("   ""type"": ""NUMBER""")
    sRequest.Append("  },")
    sRequest.Append("  {")
    sRequest.Append("   ""name"": ""Year"",")
    sRequest.Append("   ""type"": ""DATETIME""")
    sRequest.Append("  }")
    sRequest.Append(" ],")
    sRequest.Append(" ""description"": ""Insect Tracking Information."",")
    sRequest.Append(" ""isExportable"": true")
    sRequest.Append("}")

    ' Create the request
    data = encoding.GetBytes(sRequest.ToString)
    Dim req As HttpWebRequest = DirectCast(WebRequest.Create(sURL.ToString), HttpWebRequest)
    req.Method = "POST"
    req.ContentType = "application/json"
    req.ContentLength = data.Length

    ' Add the authentication header
    req.Headers.Add("Authorization: " & gCode)

    ' Send the body of the request
    Dim st As Stream = req.GetRequestStream()
    st.Write(data, 0, data.Length)
    st.Close()

    Try
        Dim res As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
        Dim sr As New StreamReader(res.GetResponseStream())
        Dim sOutput As String = sr.ReadToEnd()
        Console.WriteLine(sOutput)
    Catch ex As Exception
        Debug.Print(ex.Message)
    End Try

当我尝试创建 HttpWebResponse 时出现此错误:“远程服务器返回错误:(401) 未经授权。” 我想我正在寻找的是向 API 发布经过身份验证的请求以插入/更新/删除数据所需的代码。C# 中的示例也很好。

我已经查看了 .NET 的 Google API 客户端库,但我也无法实现它。

谢谢!

4

0 回答 0