【问题标题】:How to get LinkedIn user skills, education and positions without OAuth如何在没有 OAuth 的情况下获得 LinkedIn 用户技能、教育和职位
【发布时间】:2017-07-20 13:13:35
【问题描述】:

我正在开发一个 ASP.NET Core 应用程序,其中用户必须使用单独的数据库帐户。在所有教程中都明确指出,我必须在创建项目时选择“无身份验证”。我不明白为什么。

我想让用户能够使用 LinkedIn 登录,然后获取他们的邮件、姓名和其他信息。我可以通过:

app.UseLinkedInAuthentication(AuthenticationSettings.LinkedInOptions(
                Configuration["LinkedIn:ClientId"],
                Configuration["LinkedIn:ClientSecret"]));

在 Startup.cs 和(以及文档中的所有其他步骤)中

var loginProviders = SignInManager.GetExternalAuthenticationSchemes().ToList();
if (loginProviders.Count == 0)
{
    <div>
        <p>
            There are no external authentication services configured. See <a href="https://go.microsoft.com/fwlink/?LinkID=532715">this article</a>
            for details on setting up this ASP.NET application to support logging in via external services.
        </p>
    </div>
}
else
{
    <form asp-controller="Account" asp-action="ExternalLogin" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post" class="form-horizontal">
        <div>
            <p>
                @foreach (var provider in loginProviders)
                {
                    <button type="submit" class="btn btn-default" name="provider" value="@provider.AuthenticationScheme" title="Log in using your @provider.DisplayName account">@provider.AuthenticationScheme</button>
                }
            </p>
        </div>
    </form>
}

在_Layout.cshtml中:

我得到了按钮,用户被要求授予应用程序的权限,我得到了个人数据,但我怎样才能得到其他信息(技能、教育、职位等),例如:

How to Retrieve all possible information about a LinkedIn Account ? (API using C#)

但是没有 OAuth 或者我如何在创建项目时选择授权来实现 OAuth?

【问题讨论】:

    标签: asp.net oauth


    【解决方案1】:

    我正在使用个人数据库帐户,这就是我的工作方式:

    在 Startup.cs 中

    app.UseLinkedInAuthentication(AuthenticationSettings.LinkedInOptions(
    
                    Configuration["LinkedIn:ClientId"],
                    Configuration["LinkedIn:ClientSecret"]
    

    像这样创建一个 AuthentificationSettings 类:

    public class AuthenticationSettings
        {
          public static LinkedInAuthenticationOptions LinkedInOptions(string clientId, string clientSecret)
            {
    
                if (string.IsNullOrEmpty(clientId))
                {
                    throw new ArgumentNullException($"{nameof(clientId)} is null or empty");
                }
    
                if (string.IsNullOrEmpty(clientSecret))
                {
                    throw new ArgumentNullException($"{nameof(clientSecret)} is null or empty");
                }
    
                LinkedInAuthenticationOptions options = new LinkedInAuthenticationOptions
                {
    
                    ClientId = clientId,
                    ClientSecret = clientSecret,
                    SaveTokens = true,
                    Events = new OAuthEvents
                    {
                        OnCreatingTicket = async ctx =>
                        {
                            ctx.Identity.AddClaim(new Claim("access_token", ctx.AccessToken));
                            ctx.Identity.AddClaim(new Claim("refresh_token", ctx.AccessToken));
                            ctx.Identity.AddClaim(new Claim("ExpiresIn", ctx.ExpiresIn.Value.Seconds.ToString()));
    
    
                            // request is the GET request for linkedin
    
                            var request = new HttpRequestMessage(HttpMethod.Get, "https://api.linkedin.com/v1/people/~:(first-name,last-name,headline,picture-url,industry,summary,positions:(title,summary,start-date,end-date,company:(name,industry)))   ");
    
                            request.Headers.Authorization =new AuthenticationHeaderValue("Bearer", ctx.AccessToken);
                            request.Headers.Add("x-li-format", "json");
    
                            var response = await ctx.Backchannel.SendAsync(request, ctx.HttpContext.RequestAborted);
                            response.EnsureSuccessStatusCode();
    
                            // In user is stored the profile details
    
                            var user = JObject.Parse(await response.Content.ReadAsStringAsync());
    
    
                            // Save the linkedin details in variabels
    
                            string json = user.ToString();
    
                            LinkedinCandidate linkedinProfile = JsonConvert.DeserializeObject<LinkedinCandidate>(json);
    
                            var firstName = linkedinProfile.firstName;
                            var lastName = linkedinProfile.lastName;
                            var headline = linkedinProfile.headline;
                            var pictureUrl = linkedinProfile.pictureUrl;
                            var summaryProfile = linkedinProfile.summary;
    
                            var summaryCOmpany = linkedinProfile.positions.values[0].summary;
                            var titleJob = linkedinProfile.positions.values[0].title;
                            var companyName = linkedinProfile.positions.values[0].company.name;
                            var industry = linkedinProfile.positions.values[0].company.industry;
                            var monthStart = linkedinProfile.positions.values[0].startDate.month;
                            var yearStart = linkedinProfile.positions.values[0].startDate.year;
    
    
    
                            await Task.FromResult(0);
                        },
                        OnRemoteFailure = ctx =>
                        {
                            // Handle remote errors
    
                            return Task.FromResult(0);
                        }
                    }
    
    
                };
    
                return options;
            }
        }
    

    我设法只将几个字段添加到 json,然后用它们制作一个对象(我试图通过在 api get url 中将它们作为参数提供来获得教育和其他经验,但它没有' t 工作)。 要制作对象,您必须为此 json 创建一个模型类:

    {  "firstName": "",  
        "headline": "",  
        "lastName": "",  
        "pictureUrl": "",  
        "positions": {    
                        "_total": 1,    
                        "values": [   {        
                                        "company": {          
                                                    "industry": "",          
                                                    "name": ""        },        
                                                    "startDate": {          
                                                                    "month": ,          
                                                                    "year":         
                                                                    },        
                                                    "summary": "",        
                                                    "title": ""      
                                                    }    
                                  ] 
                    },  
        "summary": ""}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2022-11-04
      • 2015-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多