如何在没有OAuth的情况下获得LinkedIn用户的技能,教育和职位

问题描述 投票:1回答:2

我正在开发一个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,或者在创建项目时如何在选择了Authorization的情况下实现OAuth?

asp.net oauth
2个回答
2
投票

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

在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中将它们作为参数来获取知识和其他经验,但它并没有工作)。要创建对象,您必须为此json创建一个Model类:

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

0
投票

是否有任何更新可以在Blazor服务器端应用程序中实现?

我已经在验证用户身份,但是我不知道如何请求完全访问权限并检索相同的信息。

提前感谢

© www.soinside.com 2019 - 2024. All rights reserved.