gmail api返回错误400错误请求

时间:2017-09-20 14:34:04

标签: json vb.net google-api gmail

我正在尝试使用gmail API将用户设置应用到Gmail帐户,但它会一直返回错误400错误请求。

我可以看到Gmail API控制台中的错误代码及其来自我的服务帐户,所以代码不能错,但它让我发疯,只是无法弄清楚它有什么问题。

如果有人能指出我正确的方向,我会非常感激。

    `Try

        Dim Gmail_Settings_Update As HttpWebRequest
        Dim enc As UTF8Encoding
        Dim postdata As String
        Dim postdatabytes As Byte()
        Dim Gmail_User As String = "Admin_account@gmail.com"

        '
        Gmail_Settings_Update = HttpWebRequest.Create("https://www.googleapis.com/gmail/v1/users/" & HttpUtility.UrlEncode(Gmail_User) & "/settings/autoForwarding")

        enc = New System.Text.UTF8Encoding()
        postdata = "{""enabled"":""true"",""emailAddress"":""Mail.forward@me.com"",""disposition"":""leaveInInbox""}"
        postdatabytes = enc.GetBytes(postdata)

        Gmail_Settings_Update.Headers("Authorization") = "Bearer " & Token
        Gmail_Settings_Update.Method = "PUT"
        Gmail_Settings_Update.ContentType = "application/json"

        Gmail_Settings_Update.ContentLength = postdatabytes.Length

        Using stream = Gmail_Settings_Update.GetRequestStream()

            stream.Write(postdatabytes, 0, postdatabytes.Length)
        End Using
        Dim result = Gmail_Settings_Update.GetResponse()
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

1 个答案:

答案 0 :(得分:0)

我自己解决了这个问题。我需要URI中的“我”而不是用户邮件地址