0

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

根据google documentationhttps://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范围,使我可以查看验证用户的圆圈人的正确方法?

+0

这是正确的范围,你怎么知道它不工作? – DaImTo 2014-11-26 13:44:56

+0

我无法在上下文中的用户对象或标识对象中的任何其他声明中找到任何其他项目。我觉得我只是想念一些东西。 – bflemi3 2014-11-26 15:12:47

+0

可见回报列表是什么? https://developers.google.com/+/api/latest/people/list – DaImTo 2014-11-26 19:10:40

回答

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; 

     } 
找到

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

•“可见”:此用户已添加到一个或多个 圈子中的人员列表,限于发出请求的应用程序可见的圈子。

+0

我很困惑。我认为,在用户通过谷歌认证之后,范围(描述不同端点以获取关于用户的其他信息)被添加到第二个请求中,以发回请求的信息?这是不正确的?我是否向人们提供了第三个获取人们信息的请求? – bflemi3 2014-11-30 16:16:41

+0

如果您需要数据,您可以访问您需要向API发送请求。 – DaImTo 2014-11-30 16:19:22

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