VB.NET - POST request Salesforce returns 400 Bad Request error

NOTE . I have never written vb.net code before that. I googled for a solution, but did not find anything that worked.

I am trying to get an access token from Salesforce. Below code worked only yesterday . And I have no idea why it is not working today. I tried adding the content type as application / x-www-form-urlencoded , but it does not work either. When I use curl, I can get an access token. I can also get an access token using the extended leisure client in google chrome. Any ideas why it returns 400 Bad Request unknown error, retry the 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
+4
1

, -, TLS. Salesforce TLS 1.0. vb.net TLS 1.0, . , Salesforce .

, , :

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11
+10

Source: https://habr.com/ru/post/1648514/


All Articles