# Salesforce Access Token Bad Request

**URL:** <https://community.postman.com/t/salesforce-access-token-bad-request/59395>\
**Category:** Help Hub\
**Tags:** authentication, access\_token\
**Created:** [March 30, 2024, 12:27am UTC](https://community.postman.com/t/salesforce-access-token-bad-request/59395 "2024-03-30T00:27:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![avionics-physicist-5](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@avionics-physicist-5](https://community.postman.com/u/avionics-physicist-5)\
**Post date:** [March 30, 2024, 12:27am UTC](https://community.postman.com/t/salesforce-access-token-bad-request/59395/1 "2024-03-30T00:27:08Z")

</div>

I want to generate a Salesforce access token programmatically using the method below, “GetAccessTokenAsync”. I’ll then pass the token to my various methods that retrieve rows using GET.

(1) When I POST this from Postman, I get a 200 OK and an access token is returned.  
[https://ami-staging.my.salesforce.com/services/oauth2/token?grant\_type=password&client\_id=](https://ami-staging.my.salesforce.com/services/oauth2/token?grant_type=password&client_id=)&client\_secret=&username=[myorg@force.com](mailto:myorg@force.com)&password=

(2) I used the \</\> icon on Postman to generate code in c#. When I run that method from Visual Studio, I get a 400 Bad Request. I’ve triple-checked all my strings (client\_id, client\_secret, username, password) to make sure that what I’m using in Postman is identical to the inputs below. I don’t think the problem is the strings.

I would appreciate suggestions that anyone might have. Thank you!

```auto
public static async Task<string> GetAccessTokenAsync()
{
    var client = new HttpClient();

    var request = new HttpRequestMessage(HttpMethod.Post,
	"https://ami-staging.my.salesforce.com/services/oauth2/token?grant_type=password&" +
	"client_id=myid&" +
	"client_secret=mysecret&" +
	"username=myorg@force.com&" +
	"password=mypassword");

    var collection = new List<KeyValuePair<string, string>>();
    collection.Add(new("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer"));
    collection.Add(new("assertion", ""));

    var content = new FormUrlEncodedContent(collection);
    request.Content = content;

    var response = await client.SendAsync(request);

    response.EnsureSuccessStatusCode();

    await response.Content.ReadAsStringAsync();

    return "test";
}

```

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [April 29, 2024, 12:27am UTC](https://community.postman.com/t/salesforce-access-token-bad-request/59395/2 "2024-04-29T00:27:57Z")

</div>

Closed on April 29, 2024, 12:27am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
