我无法从Google Plus API获取出生日期

时间:2014-10-09 09:04:11

标签: c# windows-phone-7 google-plus

我正在尝试从google plus api获取用户信息。我正在关注Link到G +集成。在这里,我可以获得除DOB之外的所有信息。但现在我必须得到用户的出生日期。

我已查看此Linkthis link。但是现在我仍然无法获得DOB。

我已经尝试更改范围了。

https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.login

但是我在这里得到了这个错误。

The remote server returned an error: NotFound.

请帮我拿到DOB ..提前致谢..

更新问题:

如果我使用此https://www.googleapis.com/plus/v1/people/me?access_token网址,我会收到此回复:

{
 "kind": "plus#person",
 "etag": "\"Vea_b94Y77GDGgRK7gFNPnolKQw/9l0eRpGHUNy6ZNRNIEbS6cgHc3Y\"",
 "gender": "male",
 "emails": [
  {
   "value": "kathir.likes@gmail.com",
   "type": "account"
  }
 ],
 "objectType": "person",
 "id": "1024703XXXXXXXXX93",
 "displayName": "C. Vijay Dhas",
 "name": {
  "familyName": "Dhas",
  "givenName": "C. Vijay"
 },
 "url": "https://plus.google.com/1024703XXXXXXXXX93",
 "image": {
  "url": "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA
/4252rscbv5M/photo.jpg?sz=50",
  "isDefault": true
 },
 "isPlusUser": true,
 "language": "en",
 "circledByCount": 0,
 "verified": false
}

如果我使用此https://www.googleapis.com/oauth2/v1/userinfo?access_token网址,我会收到此回复:

{
 "id": "1024703XXXXXXXXX93",
 "email": "kathir.likes@gmail.com",
 "verified_email": true,
 "name": "C. Vijay Dhas",
 "given_name": "C. Vijay",
 "family_name": "Dhas",
 "link": "https://plus.google.com/1024703XXXXXXXXX93",
 "picture": "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAA
AAA/4252rscbv5M/photo.jpg",
 "gender": "male",
 "locale": "en"
}

这是我的整个代码:

public partial class GooglePlusLoginPage : PhoneApplicationPage
{
    GooglePlusTokens googlePlusTokens = new GooglePlusTokens();
    GooglePlusUserInfo googlePlusUserInfo = new GooglePlusUserInfo();

    string Parameters = null;
    string ClientId = "837597087773-9of2t72imted819fr1556t6qoj1ts6h7.apps.googleusercontent.com";
    string ClientSecret = "U8B7SdW3zmBB_k0AFCKXaE3f";
    string RedirctedUri = "http://localhost";

    [DataContract]
    public class GooglePlusAccessToken
    {
        [DataMember(Name = "access_token")]
        public string AccessToken { get; set; }
        [DataMember(Name = "refresh_token")]
        public string RefreshToken { get; set; }
        [DataMember(Name = "expires_in")]
        public string ExpiresIn { get; set; }
        [DataMember(Name = "token_type")]
        public string TokenType { get; set; }
    }

    [DataContract]
    public class GooglePlusUserProfile
    {
        [DataMember(Name = "id")]
        public string id { get; set; }
        [DataMember(Name = "email")]
        public string email { get; set; }
        [DataMember(Name = "name")]
        public string name { get; set; }
        [DataMember(Name = "given_name")]
        public string given_name { get; set; }
        [DataMember(Name = "family_name")]
        public string family_name { get; set; }
        [DataMember(Name = "link")]
        public string link { get; set; }
        [DataMember(Name = "picture")]
        public string picture { get; set; }
        [DataMember(Name = "gender")]
        public string gender { get; set; }
        [DataMember(Name = "birthday")]
        public string birthday { get; set; }

    }

    public GooglePlusLoginPage()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(GooglePlus_LoginPage_Loaded); //load google plus login page 
    }

    void GooglePlus_LoginPage_Loaded(object sender, RoutedEventArgs e)
    {
        var url = "https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=" + RedirctedUri + "&scope=https://www.googleapis.com/auth/userinfo.email%20https://www.googleapis.com/auth/userinfo.profile&client_id=" + ClientId;

        var newUrl = "https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=" + RedirctedUri + "&scope=https://www.googleapis.com/auth/plus.login%20https://www.googleapis.com/auth/userinfo.profile%20https://www.googleapis.com/auth/userinfo.email&client_id=" + ClientId;

        webBrowserGooglePlusLogin.Navigate(new Uri(url, UriKind.RelativeOrAbsolute));
    }

    private void webBrowserGooglePlusLogin_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        webBrowserGooglePlusLogin.Visibility = Visibility.Visible;
    }

    private void webBrowserGooglePlusLogin_Navigating(object sender, NavigatingEventArgs e)
    {
        if (e.Uri.Host.Equals("localhost"))
        {
            webBrowserGooglePlusLogin.Visibility = Visibility.Collapsed;
            e.Cancel = true;
            int pos = e.Uri.Query.IndexOf("=");
            string messageCode = pos > -1 ? e.Uri.Query.Substring(pos + 1) : null;
            if (messageCode != null)
            {
                Parameters = "code=" + messageCode + "&client_id=" + ClientId + "&client_secret=" + ClientSecret + "&redirect_uri=" + RedirctedUri + "&grant_type=authorization_code";
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");
                webRequest.Method = "POST";
                webRequest.ContentType = "application/x-www-form-urlencoded";
                webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest);
            }
        }
    }

    void GetRequestStreamCallback(IAsyncResult asynchronousResult)
    {
        try
        {
            HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;              
            Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);
            byte[] byteArray = Encoding.UTF8.GetBytes(Parameters);
            postStream.Write(byteArray, 0, byteArray.Length);
            postStream.Close();
            webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
        }
        catch (WebException ex)
        {
        }

    }

    void GetResponseCallback(IAsyncResult asynchronousResult)
    {
        try
        {
            var request = (HttpWebRequest)asynchronousResult.AsyncState;

            using (var resp = (HttpWebResponse)request.EndGetResponse(asynchronousResult))
            {
                using (var streamResponse = resp.GetResponseStream())
                {
                    var GooglePlusSerializerData = new DataContractJsonSerializer(typeof(GooglePlusAccessToken));
                    var GooglePlusProfileData = GooglePlusSerializerData.ReadObject(streamResponse) as GooglePlusAccessToken;
                    this.Dispatcher.BeginInvoke(
                        (Action<GooglePlusAccessToken>)((GooglePlusUserData) =>
                        {
                            GooglePlusData.AccessToken = googlePlusTokens.AccessToken = GooglePlusUserData.AccessToken;
                            googlePlusTokens.RefreshToken = GooglePlusUserData.RefreshToken;
                            googlePlusTokens.ExpiresIn = GooglePlusUserData.ExpiresIn;
                            googlePlusTokens.TokenType = GooglePlusUserData.TokenType;
                            RequestForUserProfile();
                        }), GooglePlusProfileData);

                }
            }
        }
        catch (WebException ex)
        {
            Console.WriteLine("Error==> " + ex.Message);
        }
    }

    void RequestForUserProfile()
    {
        //var urlProfile = "https://www.googleapis.com/plus/v1/people/me?access_token=" + GooglePlusData.AccessToken;
        var urlProfile = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + GooglePlusData.AccessToken;        
        WebRequest request = WebRequest.Create(urlProfile);
        request.BeginGetResponse(new AsyncCallback(this.ResponseCallbackProfile), request);
    }

    private void ResponseCallbackProfile(IAsyncResult asynchronousResult)
    {
        try
        {
            var request = (HttpWebRequest)asynchronousResult.AsyncState;

            using (var resp = (HttpWebResponse)request.EndGetResponse(asynchronousResult))
            {
                using (var streamResponse = resp.GetResponseStream())
                {

                    var GooglePlusSerializerData = new DataContractJsonSerializer(typeof(GooglePlusUserProfile));
                    var GooglePlusProfileData = GooglePlusSerializerData.ReadObject(streamResponse) as GooglePlusUserProfile;

                    this.Dispatcher.BeginInvoke(
                       (Action<GooglePlusUserProfile>)((GooglePlusUserData) =>
                      {
                          GooglePlusData.UserName = googlePlusUserInfo.UserName = GooglePlusUserData.name;
                       GooglePlusData.UserImage = googlePlusUserInfo.UserPicture = GooglePlusUserData.picture;
                         if (GooglePlusData.UserImage == null)
                          {
                              GooglePlusData.UserImage = googlePlusUserInfo.UserPicture = "https://lh3.googleusercontent.com/-_kvINpT6jtI/AAAAAAAAAAI/AAAAAAAAAAA/IEAclp4PQbk/photo.jpg";
                          }
                           googlePlusUserInfo.UserBidthday = GooglePlusUserData.birthday;
                          GooglePlusData.UserEmail = googlePlusUserInfo.UserEmail = GooglePlusUserData.email;
                         googlePlusUserInfo.UserFamilyName = GooglePlusUserData.family_name;
                        GooglePlusData.UserGender = googlePlusUserInfo.UserGender = GooglePlusUserData.gender;
                          googlePlusUserInfo.UserGivenName = GooglePlusUserData.given_name;
                         googlePlusUserInfo.UserId = GooglePlusUserData.id;
                        googlePlusUserInfo.UserLink = GooglePlusUserData.link;

                         Console.WriteLine("email==> " + GooglePlusUserData.email);
                          Console.WriteLine("birthday==> " + GooglePlusUserData.birthday);       

                           Deployment.Current.Dispatcher.BeginInvoke(() =>
                         {
                               //logout();
                            });
                       }), GooglePlusProfileData);
                }
            }
        }
        catch (WebException ex)
        {

        }
    }

    void logout()
    {
        var url = "http://accounts.google.com/Logout";
        webBrowserGooglePlusLogin.Navigate(new Uri(url, UriKind.RelativeOrAbsolute));           
        NavigationService.GoBack();           
    }
}

1 个答案:

答案 0 :(得分:0)

关于诺基亚的文章相当误导它正在做什么。特别是,虽然它说下面的方法称为&#34; Google+ API&#34;但事实并非如此。 Google+通话已被注释掉,并且调用的是旧的(已弃用的)userinfo API:


//request for user prifile
   void RequestForUserProfile()
        {
          //  var urlProfile = "https://www.googleapis.com/plus/v1/people/me?access_token=" + thisApp.AccessToken;
            var urlProfile = "https://www.googleapis.com/oauth2/v1/userinfo?access_token="+thisApp.AccessToken;
            // web request user profile
            WebRequest request = WebRequest.Create(urlProfile); 
            request.BeginGetResponse(new AsyncCallback(this.ResponseCallbackProfile), request);
        }

如果您使用&#34; userinfo&#34;注释掉urlProfile;在路径中并使用&#34; plus / v1 / people&#34;取消注释,您可能会有更好的运气。

如果这不起作用,您可能需要更新原始问题,以准确显示您正在使用的代码,您尝试执行的操作以及您错误消息的确切位置获得生成。