VB.NET - Salesforce POST请求返回400错误请求错误 [英] VB.NET - Salesforce POST Request returns 400 Bad Request Error

查看:252
本文介绍了VB.NET - Salesforce POST请求返回400错误请求错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

NOTE: I've never written vb.net code before this. I've googled for a solution but did not find anything that worked.

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

I'm trying to get access token from Salesforce. Below code worked just yesterday. And I have no idea why it is not working today. I've tried adding content-type as application/x-www-form-urlencoded but it did not work either. When I use curl I'm able to get access token. Also I'm able to get access token using advanced rest client in google chrome. Any ideas why it is returning 400 Bad Request unknown error retry your request?

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


推荐答案

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

Well, appearently problem was about TLS version mismatch. All Salesforce sandboxes refuse TLS 1.0 connections. Our vb.net test code was using TLS 1.0 thus returning an error. It would be great if Salesforce would return better error code.

我需要做的就是在代码块之上添加一行代码:

All I needed to do was add a line of code on top of the code block:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11

这篇关于VB.NET - Salesforce POST请求返回400错误请求错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆