微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

VB.NET – Salesforce POST请求返回400错误请求错误

注意:我之前从未编写过vb.net代码.我已经google了解决方案,但没有发现任何有效的方法.

我正在尝试从Salesforce获取访问令牌.下面的代码就在昨天工作.我不知道为什么它今天不起作用.我已经尝试添加内容类型作为application / x-www-form-urlencoded,但它也没有用.当我使用curl时,我能够获得访问令牌.此外,我可以使用谷歌浏览器中的高级休息客户端获取访问令牌.任何想法为什么它返回400错误请求未知错误重试您的请求?

Imports System.Collections.Specialized
Imports System.Net
Imports System.Text

Module Module1

Sub Main()
    Dim clientId As String = "clientId"
    Dim clientSecret As String = "clientSecret"
    Dim redirectUri As String = "https://test.salesforce.com"
    Dim environment As String = "https://test.salesforce.com"
    Dim tokenUrl As String = ""
    Dim username As String = "[email protected]"
    Dim password As String = "passwordtoken"
    Dim accesstoken As String = ""
    Dim instanceUrl As String = ""

    Console.WriteLine("Getting a token")

    tokenUrl = environment + "/services/oauth2/token"
    Dim request As WebRequest = WebRequest.Create(tokenUrl)

    Dim values As NameValueCollection = New NameValueCollection()
    values.Add("grant_type","password")
    values.Add("client_id",clientId)
    values.Add("client_secret",clientSecret)
    values.Add("redirect_uri",redirectUri)
    values.Add("username",username)
    values.Add("password",password)

    request.Method = "POST"

    Try
        Dim client = New WebClient()
        Dim responseBytes As Byte() = client.UploadValues(tokenUrl,"POST",values)
        Dim response As String = Encoding.UTF8.GetString(responseBytes)
        Console.WriteLine(response)
        Console.ReadKey()
    Catch ex As Exception
        Console.WriteLine(ex.Message)
        Console.WriteLine("Press any key to close")
        Console.ReadKey()
    End Try

End Sub

End Module

解决方法

好吧,显然问题是关于TLS版本不匹配.所有Salesforce沙箱都拒绝TLS 1.0连接.我们的vb.net测试代码使用的是TLS 1.0,因此返回错误.如果Salesforce会返回更好的错误代码,那就太好了.

我需要做的就是在代码块的顶部添加一行代码

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐