Google OAuth2使用plus.login范围来检索带圆圈的人

时间:2014-11-23 16:14:09

标签: c# authentication google-plus google-oauth google-api-dotnet-client

我正在通过Microsoft.Owin库使用OAuth2身份验证构建MVC 5应用程序,以通过google(google +)进行身份验证。我可以添加email范围,但我尝试添加plus.login范围以请求用户个人资料信息(即:他们圈子中的人)不会在响应中返回任何额外信息({ {1}}或User)。

根据google documentationIdentity范围添加到身份验证请求中,应提供对社交功能的访问权限,例如圈出的人员列表。但是,我没有通过添加https://www.googleapis.com/auth/plus.login范围获得任何额外的声明。

Startup.Auth.cs

plus.login

请求var googleOptions = new GoogleOAuth2AuthenticationOptions { ClientId = "xxx", ClientSecret = "xxx", Provider = new GoogleOAuth2AuthenticationProvider { OnAuthenticated = async context => { context.Identity.AddClaim(new Claim("Image", context.User.GetValue("image").ToString())); // profile doesn't exist in the User object context.Identity.AddClaim(new Claim("Profile", context.User.GetValue("profile").ToString())); } } }; googleOptions.Scope.Add("https://www.googleapis.com/auth/plus.login"); googleOptions.Scope.Add("email"); app.UseGoogleAuthentication(googleOptions); 范围的正确方法是什么,以便我可以查看经过身份验证的用户的圈定人员?

2 个答案:

答案 0 :(得分:1)

请尝试以下代码:

var googleOptions = new GoogleOAuth2AuthenticationOptions {
    ClientId = "xxx",
    ClientSecret = "xxx",
    Provider = new GoogleOAuth2AuthenticationProvider {
        OnAuthenticated = async context => {
            context.Identity.AddClaim(new Claim("Image", context.User.GetValue("image").ToString()));
            // profile doesn't exist in the User object
            context.Identity.AddClaim(new Claim("Profile", context.User.GetValue("profile").ToString()));
        }
    }
};
googleOptions.Scope.Add("email+https://www.googleapis.com/auth/plus.login");
app.UseGoogleAuthentication(googleOptions);

答案 1 :(得分:0)

以下示例列出了与当前经过身份验证的用户关联的人员。

它使用当前的Google-dotnet-Client lib

  

// PM>安装包Google.Apis.Plus.v1

直接从Git Hub Google-Dotnet-Samples/Google-plus/Diamto-Google-plus-sample翻录的代码可以在Google+ API List with C#找到与之相关的教程

#region Person
        /// <summary>
        /// List all of the people in the specified collection
        /// documentation:  https://developers.google.com/+/api/latest/people/list
        /// </summary>
        /// <param name="service"></param>
        /// <param name="_userId">Get the collection of people for the person identified. Use "me" to indicate the authenticated user.</param>
        /// <returns></returns>
        public static IList<Person> GetAllPeople(PlusService service, string _userId)
        {
            PeopleResource.ListRequest list = service.People.List(_userId, PeopleResource.ListRequest.CollectionEnum.Visible);
            list.MaxResults = 10;
            PeopleFeed peopleFeed = list.Execute();
            IList<Person> people = new List<Person>(); 

            //// Loop through until we arrive at an empty page
            while (peopleFeed.Items != null)
            {
                // Adding each item  to the list.
                foreach (Person item in peopleFeed.Items)
                {
                    people.Add(item);
                }

                // We will know we are on the last page when the next page token is
                // null.
                // If this is the case, break.
                if (peopleFeed.NextPageToken == null)
                {
                    break;
                }

                // Prepare the next page of results
                list.PageToken = peopleFeed.NextPageToken;

                // Execute and process the next page request
                peopleFeed = list.Execute();

            }

            return people;

        }

因此,假设您的用户在上方正确进行了身份验证,您应该可以访问人员列表中的用户。

  

•&#34;可见&#34;:此用户已添加到一个或多个人的人员列表   圆圈,仅限于请求应用程序可见的圆圈。