【问题标题】:Sending JSON data from Logic App to Function App to Eloqua with RestSharp使用 RestSharp 将 JSON 数据从 Logic App 发送到 Function App 到 Eloqua
【发布时间】:2021-06-16 04:46:52
【问题描述】:

我正在尝试制作一个函数应用,它从逻辑应用获取 JSON 数据,然后将其发送到 Eloqua API。

我一直在用the Eloqua documentation in order to set it up.

但是,我遇到了问题。

我似乎没有从我的逻辑应用程序中获取任何数据,或者无法解析从您发送的第一个 OAuth2 网址获得的网址。

我想知道是否有人知道我犯了什么错误,因为我在这方面有点初级,并不完全确定我做错了什么。

这是我用于我的函数应用程序的代码: 主要功能

/*
             * 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; }
}

提前谢谢你。

【问题讨论】:

  • 根据代码,使用授权码流程获取token,然后调用API。我们不能在 Azure 函数中这样做。因为如果我们使用这种方式,我们需要打开一个页面来输入用户名和密码。但是Azure函数无法打开页面..所以我建议你使用resource owner password credentials grant流来获取令牌并调用API
  • 如何实现才能使用resource owner password credentials grant?对不起,如果这是一个愚蠢的问题。

标签: c# azure-logic-apps restsharp azure-function-app eloqua


【解决方案1】:

如果您想使用资源所有者密码凭证授权流程,请参考以下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"
}
                

代码

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 2014-01-11
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多