Search code examples
c#httpsdotnet-httpclientexacttargetsalesforce-marketing-cloud

Salesforce marketing cloud API does not return an access/authorization token


I have an application that requests the marketing cloud for an access token to use in subsequent requests to access the API. This was designed last year and working fine till last week. The same request has been failing since 02/22. Here is the sample code to request the token:

public static async Task<string> GetAuthorizationToken(string ClientId, string ClientSecret)
    {
        string strAuthorizationToken = string.Empty;
        HttpClient client = new HttpClient();

        var dictParams = new Dictionary<string, string>()
        {
            { "clientId", ClientId }, {"clientSecret", ClientSecret }
        };
        var content = new FormUrlEncodedContent(dictParams);

        var response = await client.PostAsync("https://auth.exacttargetapis.com/v1/requestToken", content);
        if (response.IsSuccessStatusCode)
        {
            var strresponse = await response.Content.ReadAsStringAsync();
            //dynamic objResult = JsonConvert.DeserializeObject<dynamic>(strresponse);
            //strAuthorizationToken = objResult.accessToken;
        }

        return strAuthorizationToken;
    }

GetAuthorizationToken("*********", "*******").GetAwaiter().GetResult();

Here is the error I am getting from the API:

{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  X-Mashery-Responder: 02-26
  Vary: Origin
  X-Mashery-Message-ID: f4cec199-2e7e-49e2-88e0-6673ffe849ed
  strict-transport-security: max-age=15552000; preload
  Content-Security-Policy: upgrade-insecure-requests
  x-xss-protection: 1; mode=block
  x-frame-options: DENY
  x-content-type-options: nosniff
  Referrer-Policy: strict-origin-when-cross-origin
  Connection: close
  Cache-Control: no-store, must-revalidate, no-cache, max-age=0, private
  Date: Tue, 26 Feb 2019 16:42:29 GMT
  Server: Apache
  Content-Length: 223
  Content-Type: application/json; charset=UTF-8
}}

I need to know what has been changed or what I need to fix in this code to be able to access the API again. Please help how I can make this work again. Thanks in advance.


Solution

  • I am posting the answer as it is resolved and might be helpful for someone else who has a similar issue with sending http requests.

    I had to add the following line of code to specify the security protocol to use:

    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls;