0

I'm trying to write a small SFMC API so that I can integrate it with our SQL server. After a few hours of failing to get this working in a SQL procedure (which would be ideal) I decided to give .NET a try.

I'm trying to get my RequestToken so I can read and write to DataExtensions. RequestToken Docs here: https://developer.salesforce.com/docs/atlas.en-us.mc-getting-started.meta/mc-getting-started/requestToken.htm

I installed FuelSDK through NuGet, but I am not able to get this working in C#. Although I'm new to .NET, I have experience with C#. I'm sure I'm almost there. My method is below:

    [HttpGet("/sfmc/requestToken/")]
    public async Task<string> RequestToken()
    {
        string endpoint = "https://auth.exacttargetapis.com/v1/requestToken"; 
        string clientId = "asdfasdfasdfasdfasdfasdfasdf";
        string clientSecret = "asdfasdfasdfasdfasdfasdfasdf";

        using (var client = new HttpClient())
        {
            var values = new Dictionary<string, string>
            {
                { "clientId", clientId },
                { "clientSecret", clientSecret }
            };

            var content = new FormUrlEncodedContent(values);
            var response = await client.PostAsync(new Uri(endpoint), content);
            return await response.Content.ReadAsStringAsync();
        }

    }

Thanks in advance!

  • 1
    Can you give some more details about what exactly is not working? Is there a specific exception that you are getting? – Ryan Rudd Jan 10 '18 at 21:27
  • Thanks for reaching out. I was able to solve this issue in SFMC. This code is okay. – brandoncluff Jan 10 '18 at 22:36
0

Working code:

    string endpoint = "https://auth.exacttargetapis.com";

    [HttpGet("/sfmc/requestToken/")]
    public async Task<string> RequestToken()
    {

        string clientId = "asdfasdfasdfasdfasdfasdfasdf";
        string clientSecret = "asdfasdfasdfasdfasdfasdfasdf";

        using (var client = new HttpClient())
        {
            var values = new Dictionary<string, string>
            {
                { "clientId", clientId },
                { "clientSecret", clientSecret }
            };

            var content = new FormUrlEncodedContent(values);
            var response = await client.PostAsync(new Uri(endpoint + "/v1/requestToken"), content);
            string responseString = await response.Content.ReadAsStringAsync();
            dynamic responseObject = JObject.Parse(responseString);

            return (string) responseObject.accessToken;
        }
    }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.