【问题标题】:Unable to retrieve LinkedIn Authorization Code无法检索 LinkedIn 授权代码
【发布时间】:2018-12-16 20:30:03
【问题描述】:

我正在构建一个应用程序,用于在 LinkedIn 上发布帖子。我正在使用 Visual Studio 2017 (asp.net mvc c#)。 我已经在 LinkedIn 中创建了一个带有客户 ID 和客户密码的应用程序。 我还在 Visual Studio 中创建了一个异步函数 GetCode() 我的应用程序。

这个函数向这个 url 发出请求: 字符串 url = "https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=kjahldj9384&state=DCEEFWKDHIUs5dffef424&redirect_uri=http://localhost:51272/";

当我运行应用程序并调用此函数时,我被重定向到 LinkedIn 身份验证页面进行登录。但是当我输入 LinkedIn 登录凭据并点击登录时,它会将我带到此页面:https://www.linkedin.com/uas/login-submit,其中显示错误消息:

请求错误 很抱歉,您的请求有问题。请确保您已启用 cookie,然后重试。

或点击此链接返回主页。 这是我的 GetCode 函数:

        //Get LinkedIn Code
    [HttpGet]
    [Route("api/LinkedIn/GetCode")]
    public async Task<HttpResponseMessage> GetCode()
    {
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Content-Type", "application/x-www-form-urlencoded");

     string url = "https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=54565kggfh&state=DCEEFW55754FD5dffef424&redirect_uri=http://localhost:57313/";

            HttpResponseMessage apiResponseMsg = await client.GetAsync(new Uri(url));

            return apiResponseMsg;
        }
    }

是不是我做错了什么?

【问题讨论】:

    标签: oauth-2.0 linkedin


    【解决方案1】:

    首先,确保将您的 redirect_uri 添加到 Linkedin 开发者门户中的 OAuth 2.0 设置(在 Auth 选项卡下)。如果这不是问题,请在下面尝试。

    如果您构建相同的 URL,并将页面重定向到它而不是使用响应消息,那么您应该使用代码作为查询参数 (http://localhost:60137/?code=xxxxxxxxxxxxx) 被发送回您的网站。我用一个测试应用程序做了一个工作示例:

        [HttpGet]
        public ActionResult GetCode()
        {
            //build url from config file
            string url = "https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=78z99sg1ncgbma&redirect_uri=http://localhost:60137";
    
            return new RedirectResult(url);
        }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-10
      • 1970-01-01
      • 2020-06-12
      • 1970-01-01
      • 1970-01-01
      • 2012-12-24
      相关资源
      最近更新 更多