Sending JSON data from Logic App to Function App to Eloqua with RestSharp

171 Views Asked by At

I'm trying to make a Function App that takes JSON data from a Logic App and then sends it to Eloqua API.

I've been using the Eloqua documentation in order to set it up.

However, I am getting in to problems.

I seem to not get any data from my Logic App or be able to parse the URL you get from the first OAuth2 url you send.

I'm wondering if anyone knows what mistakes I've made as I'm kind of a junior on this and not entirely sure what I did wrong.

This is the code that I am using for my Function App: Main function

/*
             * Sets up the needed variables for sending to Eloqua
             */
            string clientSecreat = "xxxxx";
            var authentication = System.Text.Encoding.UTF8.GetBytes(clientSecreat);
            string getJson = req.GetQueryNameValuePairs()
                            .FirstOrDefault(q => string.Compare(q.Key, "Body", true) == 0)
                            .Value;

            /*
             * Starts by calling to get token for Oauth2
             */
            var client = new RestClient("https://login.eloqua.com/auth/oauth2/authorize?");
            var request = new RestRequest(Method.GET);
            var queryResult = client.Execute<Token>(request);
            //log.Info(queryResult.ToString());


            client = new RestClient("https://login.eloqua.com/auth/oauth2/token");
            request = new RestRequest(Method.POST);
            request.RequestFormat = DataFormat.Json;
            request.AddHeader("Authorization", "Basic " + authentication);
            request.AddJsonBody("grant_type: ", "authorization_code");
            request.AddJsonBody("code: " + queryResult);
            request.AddJsonBody("redirect_uri: ", "redirect_url");
            var response = await client.ExecuteAsync<AccessToken>(request);
            log.Info(response.ToString());

            /*
             * Posts the Logic App parsed JSON data
             */
            client = new RestClient("https://secure.p06.eloqua.com/API/Bulk/2.0/customObjects/377/imports/1904470/data");
            request = new RestRequest(Method.POST);
            request.RequestFormat = DataFormat.Json;
            request.AddHeader("Content_Type: ", "application/json");
            request.AddHeader("Authorization: ", "Basic " + response);
            request.AddJsonBody(getJson);
            var execute = await client.ExecuteAsync(request);

            return req.CreateResponse("Success");

Token.cs:

public class Token
{

    [JsonProperty("code")]
    public string code { get; set; }


}

AccessToken.cs:

    public class AccessToken
{
    [JsonProperty("access_token")]
    public string access_token { get; set; }
}

Thank you in advance.

1

There are 1 best solutions below

0
Jim Xu On

If you want to use resource owner password credentials grant flow, please refer to the following rest API


POST https://login.eloqua.com/auth/oauth2/token
Authorization: Basic <base64 encode string client_id:client_secret>
{
   "grant_type":"password",
   "scope":"full",
   "username":"testsite\\testuser",
   "password":"user123"
}
                

code

var authentication = Convert.ToBase64String(Encoding.UTF8.GetBytes("client_id:client_secret"));
client = new RestClient("https://login.eloqua.com/auth/oauth2/token");
            request = new RestRequest(Method.POST);
            request.RequestFormat = DataFormat.Json;
            request.AddHeader("Authorization", "Basic " + authentication);
            ....body
            var response = await client.ExecuteAsync<AccessToken>(request);
            log.Info(response.ToString());