0

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.

Ravoos
  • 1
  • 1
  • 6
  • According to the code, you use the authorization code flow to get token then call API. We cannot do that in Azure function. Because if we use the way, we need to open a page to enter username and password. But Azure function cannot open a page.. So I suggest you use `resource owner password credentials grant` flow to get token and call API – Jim Xu Mar 19 '21 at 06:55
  • How would that be implemented to be able to use `resource owner password credentials grant`? Sorry if this is a stupid question. – Ravoos Mar 19 '21 at 10:15

1 Answers1

0

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());

Jim Xu
  • 21,610
  • 2
  • 19
  • 39