tug*_*gce 4 vb.net oauth salesforce http-post visual-studio-2015
注意:我之前从未编写过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 = "username@salesforce.com"
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
Run Code Online (Sandbox Code Playgroud)
tug*_*gce 10
好吧,显然问题是关于TLS版本不匹配.所有Salesforce沙箱都拒绝TLS 1.0连接.我们的vb.net测试代码使用TLS 1.0,因此返回错误.如果Salesforce会返回更好的错误代码,那就太好了.
我需要做的就是在代码块的顶部添加一行代码:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11
Run Code Online (Sandbox Code Playgroud)