1

Google Fusion Table を更新するために、現在は廃止されている Fusion Tables SQL API をしばらく使用してきました。リクエストを認証して必要なタスクを実行する 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) Unauthorized.」というエラーが表示されます。私が探しているのは、認証されたリクエストをAPIに投稿してデータを挿入/更新/削除するために必要なコードだと思います。C# の例も問題ありません。

.NET 用の Google API クライアント ライブラリを調べましたが、それも実装できません。

ありがとう!

4

0 に答える 0