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

本文关键字:检索 范围 OAuth2 使用 plus login Google | 更新日期: 2023-09-27 18:31:31

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

根据谷歌文档,将https://www.googleapis.com/auth/plus.login范围添加到身份验证请求中应该提供对社交功能的访问,例如他们圈出的人的列表。但是我没有通过添加plus.login范围返回任何额外的声明。

Startup.Auth.cs

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);

请求plus.login范围以便我可以查看经过身份验证的用户的圈出人员的正确方法是什么?

Google OAuth2 使用 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("email+https://www.googleapis.com/auth/plus.login");
app.UseGoogleAuthentication(googleOptions);
以下示例

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

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

/

/PM> install-Package 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;
        }

因此,假设您的用户在上面经过了正确的身份验证,您应该有权访问人员列表中的用户。

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