【问题标题】:How to request OAuth2 token to kong plugin如何向kong插件请求OAuth2令牌
【发布时间】:2020-03-03 22:59:27
【问题描述】:

我正在关注本教程https://medium.com/@far3ns/kong-oauth-2-0-plugin-38faf938a468,当我请求令牌时

Headers: Content-Type:application/json
Host:api.ct.id
Body:
{
“client_id”: “CLIENT_ID_11”,
“client_secret”: “CLIENT_SECRET_11”,
“grant_type”: “password”,
“provision_key”: “kl3bUfe32WBcppmYFr1aZtXxzrBTL18l”,
“authenticated_userid”: “oneone@gmail.com”,
“scope”: “read”
} 

返回

{
  "error_description": "Invalid client authentication",
  "error": "invalid_client"
}

无论我尝试什么我都无法修复它,知道如何让它正常工作

【问题讨论】:

    标签: python-3.x http https oauth-2.0 kong


    【解决方案1】:

    你需要创建kong开发者,它会给你client_id和client_secret_Id。在生成身份验证令牌时使用这些值。

    【讨论】:

    • 请考虑添加指向相关注册页面的链接,并花时间扩展可以找到参数的位置。此外,通过将变量包装在 ` 中,您可以以代码样式的字体显示它们。
    【解决方案2】:

    这是工作的 c# 代码。

    选项 1

    public static string GetOAuthToken(string url, string clientId, string clientSecret, string scope = "all", string grantType = "client_credentials")
            {
                try
                {
                    string token = "";
                    if (string.IsNullOrWhiteSpace(url)) throw new ArgumentException("message", nameof(url));
                    if (string.IsNullOrWhiteSpace(clientId)) throw new ArgumentNullException("message", nameof(clientId));
                    if (string.IsNullOrWhiteSpace(clientSecret)) throw new ArgumentNullException("message", nameof(clientSecret));
    
                    var oAuthClient = new RestClient(new Uri(url));
                    var request = new RestRequest("Authenticate", Method.POST);
    
                    request.AddHeader("Content-Type", "application/json");
    
                    var credentials = new
                    {
                        grant_type = grantType,
                        scope = scope,
                        client_id = clientId,
                        client_secret = clientSecret
                    };
    
                    request.AddJsonBody(credentials);
    
                    var response = oAuthClient?.Execute(request);
                    var content = response?.Content;
    
                    if (string.IsNullOrWhiteSpace(content)) throw new ArgumentNullException("message", nameof(clientSecret));
                    token = content?.Trim('"');
    
                    return token;
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message,ex);
                }
            }
    

    选项 2

    var httpClient = new HttpClient()
    var creds = $"client_id={client_id}&client_secret{client_secret}&grant_type=client_credentials";
    httpClient.DefaultRequestHeaders.Accept.Clear();
    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
    var content = new StringContent(creds, Encoding.UTF8, "application/x-www-form-urlencoded");
    var response = httpClient.PostAsync("https://myorg/oauth/oauth2/cached/token", content).Result;
    var OAuthBearerToken = response.Content.ReadAsStringAsync().Result;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-11
      • 2019-09-13
      • 2018-08-13
      • 2012-06-08
      • 1970-01-01
      • 2019-11-19
      • 1970-01-01
      • 2023-01-14
      相关资源
      最近更新 更多