title: Google Login and retrieving profile info in Windows Phone 8.1 WinRT app
body: <p>I am trying to add Google login in my universal app,For Windows 8.1 app,it's easy to do but in case of Windows Phone 8.1 WinRT app,
  Here is the code I did:</p>
  
  <pre><code>      private String simpleKey = "YOUR_SIMPLE_API_KEY"; // Should keep this secret
              private String clientID = "ffffff-     n12s9sab94p3j3vp95sdl7hrm2lbfk3e.apps.googleusercontent.com";
              private string CALLBACKuri = "writeprovidedcallbackuri";
             private String clientSecret = "LYffff2Q6MbgH623i"; // Keep it secret!
             private String callbackUrl = "urn:ietf:wg:oauth:2.0:oob";
  
             private String scope = "https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.email";
           public GooglePlusLoginPage()
          {
              this.InitializeComponent();
              refreshToken = null;
              code = null;
              access_token = null;
              renderArea = this;
              Auth();
  
          }
  
    public  void Auth()
          {
              Windows.Storage.ApplicationData.Current.LocalSettings.Values["code"] = "";
              if (access_token == null)
              {
                  if (refreshToken == null &amp;&amp; code == null)
                  {
                      try
                      {
                          String GoogleURL = "https://accounts.google.com/o/oauth2/auth?client_id=" + Uri.EscapeDataString(clientID) + "&amp;redirect_uri=" + Uri.EscapeDataString(callbackUrl) + "&amp;response_type=code&amp;scope=" + Uri.EscapeDataString(scope);
  
                          System.Uri StartUri = new Uri(GoogleURL);
                          // When using the desktop flow, the success code is displayed in the html title of this end uri
                          System.Uri EndUri = new Uri("https://accounts.google.com/o/oauth2/approval?");
  
  
                          WebAuthenticationBroker.AuthenticateAndContinue(StartUri, EndUri, null, WebAuthenticationOptions.None);
                          //  await Task.Delay(2);
  
                      }
                      catch (Exception Error)
                      {
  
  
                          ((GooglePlusLoginPage)renderArea).SendToLangingPage();
  
                      }
                  }
              }
              //codeToAcccesTok();
          }
          /// &lt;summary&gt;
          /// Invoked when this page is about to be displayed in a Frame.
          /// &lt;/summary&gt;
          /// &lt;param name="e"&gt;Event data that describes how this page was reached.  The Parameter
          /// property is typically used to configure the page.&lt;/param&gt;
          protected override void OnNavigatedTo(NavigationEventArgs e)
          {
              string name = e.Parameter as string;
              IsGplusLogin = true;
  
  
  
  
              // When the navigation stack isn't restored navigate to the ScenarioList
  
  
          }
          private void OutputToken(String TokenUri)
          {
              string access_token = TokenUri;
          }
  
          public void ContinueWebAuthentication(WebAuthenticationBrokerContinuationEventArgs args)
          {
  
              WebAuthenticationResult result = args.WebAuthenticationResult;
  
              if (result.ResponseStatus == WebAuthenticationStatus.Success)
              {
  
                  string response = result.ResponseData.ToString();
  
                  code = response.Substring(response.IndexOf("=") + 1);
                  Windows.Storage.ApplicationData.Current.LocalSettings.Values["code"] = code;
                  // TODO: switch off button, enable writes, etc.
              }
              else if (result.ResponseStatus == WebAuthenticationStatus.ErrorHttp)
              {
                  //TODO: handle WebAuthenticationResult.ResponseErrorDetail.ToString()
              }
              else
              {
                  ((GooglePlusLoginPage)renderArea).SendToLangingPage();
                  // This could be a response status of 400 / 401
                  // Could be really useful to print debugging information such as "Your applicationID is probably wrong"
                  //TODO: handle WebAuthenticationResult.ResponseStatus.ToString()
              }
              codeToAcccesTok();
          }
          interface IWebAuthenticationContinuable
          {
              /// &lt;summary&gt;
              /// This method is invoked when the web authentication broker returns
              /// with the authentication result
              /// &lt;/summary&gt;
              /// &lt;param name="args"&gt;Activated event args object that contains returned authentication token&lt;/param&gt;
              void ContinueWebAuthentication(WebAuthenticationBrokerContinuationEventArgs args);
          }
  
  
    private async void codeToAcccesTok()
          {
  
  
              string oauthUrl = "https://accounts.google.com/o/oauth2/token";
  
              HttpClient theAuthClient = new HttpClient();
  
              HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, oauthUrl);
  
              // default case, we have an authentication code, want a refresh/access token            
              string content = "code=" + code + "&amp;" +
                  "client_id=" + clientID + "&amp;" +
                  "client_secret=" + clientSecret + "&amp;" +
                  "redirect_uri=" + callbackUrl + "&amp;" +
                  "grant_type=authorization_code";
  
  
              if (refreshToken != null)
              {
                  content = "refresh_token=" + refreshToken + "&amp;" +
                  "client_id=" + clientID + "&amp;" +
                  "client_secret=" + clientSecret + "&amp;" +
                  "grant_type=refresh_token";
              }
  
              request.Method = HttpMethod.Post;
              request.Content = new StreamContent(new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(content)));
              request.Content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
  
              try
              {
                  HttpResponseMessage response = await theAuthClient.SendAsync(request);
                  parseAccessToken(response);
              }
              catch (HttpRequestException)
              {
  
              }
          }
  
          public async void parseAccessToken(HttpResponseMessage response)
          {
              string content = await response.Content.ReadAsStringAsync();
              //content="{\n  \"error\" : \"invalid_request\",\n  \"error_description\" : \"Missing required parameter: code\"\n}";
              if (content != null)
              {
                  string[] lines = content.Replace("\"", "").Replace(" ", "").Replace(",", "").Split('\n');
                  for (int i = 0; i &lt; lines.Length; i++)
                  {
                      string[] paramSplit = lines[i].Split(':');
                      if (paramSplit[0].Equals("access_token"))
                      {
                          access_token = paramSplit[1];
                      }
                      if (paramSplit[0].Equals("refresh_token"))
                      {
                          refreshToken = paramSplit[1];
                          Windows.Storage.ApplicationData.Current.LocalSettings.Values["refreshToken"] = refreshToken;
                      }
                  }
                  //access_token="ya29.aAAvUHg-CW7c1RwAAACtigeHQm2CPFbwTG2zcJK-frpMUNqZkVRQL5q90mF_bA";
  
                  if (access_token != null)
                  {
  
                      getProfile();
                  }
                  else
                  {
                      ((GooglePlusLoginPage)renderArea).SendToLangingPage();
  
                      // something is wrong, fix this
                  }
              }
  
          }
          private async void ParseProfile(HttpResponseMessage response)
          {
              string content = await response.Content.ReadAsStringAsync();
              if (content != null)
              {
                  var serializer = new DataContractJsonSerializer(typeof(UserEmail));
                  UserInfo = serializer.ReadObject(new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(content))) as UserEmail;
                  ((GooglePlusLoginPage)renderArea).RenderUser();
                  WebView wb = new WebView();
                  var url = "http://accounts.google.com/Logout";
                  wb.Navigate(new Uri(url, UriKind.RelativeOrAbsolute));
  
  
              }
          }
  
          public async void getProfile()
          {
  
  
  
              httpClient = new HttpClient();
  
              var searchUrl = "https://www.googleapis.com/oauth2/v2/userinfo";
  
              httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + access_token);
  
              try
              {
                  HttpResponseMessage response = await httpClient.GetAsync(searchUrl);
                  ParseProfile(response);
              }
              catch (HttpRequestException hre)
              {
                  // DebugPrint(hre.Message);
              }
          }
  
      public async void RenderUser()
          {
              GridProgressRing.Visibility = Visibility.Visible;
  
              Imageuri = UserInfo.picture.ToString().Replace("sz=50", "sz=150");
              displayname = UserInfo.name;
              Google_Id = UserInfo.id;
              emailid = UserInfo.Email;
              first_name = displayname;
              uniqueName = Imageuri.ToString();
              string Imagefile = "";
              if (ShareMenuClass.CheckInternetConnection())
              {
                  Imagefile = await ShareMenuClass.ToBase64String(Imageuri);
              }
              if (first_name.Contains(' '))
              {
                  string[] dfdf = new string[2];
                  dfdf = first_name.Split(' ');
                  first_name = dfdf[0];
                  last_name = dfdf[1];
              }
  
              password = "google user";
              string DataString = "&lt;UserRegistration&gt;" + "&lt;FirstName&gt;" + first_name + "&lt;/FirstName&gt;&lt;LastName&gt;" + last_name + "&lt;/LastName&gt;&lt;Platform&gt;Windows 8&lt;/Platform&gt;" +
              "&lt;UUID&gt;" + getDeviceId() + "&lt;/UUID&gt;&lt;EmailId&gt;" + emailid + "&lt;/EmailId&gt;&lt;Password&gt;" + password + "&lt;/Password&gt;&lt;Photo&gt;" + Imagefile +
              "&lt;/Photo&gt;&lt;OrganiztionName&gt;" + organization_name + "&lt;/OrganiztionName&gt;&lt;Location&gt;indore&lt;/Location&gt;&lt;AppId&gt;2&lt;/AppId&gt;&lt;querytype&gt;register&lt;/querytype&gt;&lt;/UserRegistration&gt;";
              if (ShareMenuClass.CheckInternetConnection())
              {
                  string Front = "&lt;UserRegistration xmlns=\"www.XMLWebServiceSoapHeaderAuth.net\"&gt; &lt;UserRegistrationXml&gt;";
                  string Back = "&lt;/UserRegistrationXml&gt;&lt;/UserRegistration&gt;";
                  DataString = DataString.Replace("&lt;", "&amp;#60;");
                  DataString = DataString.Replace("&gt;", "&amp;#62;");
                  DataString = Front + DataString + Back;
                  string RecivedString = await ShareMenuClass.CallWebService("UserRegistration", DataString);
                  bool flagtoFillDefaultProgress = true;
                  if (RecivedString.Contains("Email Id is already registered"))
                  {
                      flagtoFillDefaultProgress = false;
  
                      string SoapXml = "&lt;getuserProgressInfo&gt;&lt;EmailId&gt;" + emailid + "&lt;/EmailId&gt;&lt;AppId&gt;2&lt;/AppId&gt;&lt;/getuserProgressInfo&gt;";
                      Front = "&lt;getuserProgress xmlns=\"www.XMLWebServiceSoapHeaderAuth.net\"&gt;&lt;getuserProgressInfoXml&gt;";
                      Back = "&lt;/getuserProgressInfoXml&gt;&lt;/getuserProgress&gt;";
                      SoapXml = SoapXml.Replace("&lt;", "&amp;#60;");
                      SoapXml = SoapXml.Replace("&gt;", "&amp;#62;");
                      SoapXml = Front + SoapXml + Back;
  
                      RecivedString = await ShareMenuClass.CallWebService("getuserProgress", SoapXml);
                  }
                  if (RecivedString.Contains("success"))
                  {
                      txtplswait.Text = "Configuring your account...";
  
                      RecivedXml.RecivedStringToObserCollection(RecivedString);
                      //if (flagtoFillDefaultProgress)
                      //{
                      await System.Threading.Tasks.Task.Delay(25);
  
                      await RecivedXml.FillMyHalfList();
                      //}
                      RecivedXml.SerializeRecivedRecivedollection();
                      ShareMenuClass.Google_Loging = true;
                      if (RecivedXml.WholeRecivedData[0].response == "success")
                      {
                          StorageFile storagefile = await ApplicationData.Current.LocalFolder.CreateFileAsync("IsGoogleUser.txt", CreationCollisionOption.ReplaceExisting);
  
                          RecivedXml.SerializeSignedUserInfo(RecivedXml.WholeRecivedData[0].Id);
  
                          Quizstatemodleobj.GetOverallQuizProgressForAllUserAndFillThisUserList(RecivedXml.WholeRecivedData[0].Id);
                          await System.Threading.Tasks.Task.Delay(25);
  
                          GridProgressRing.Visibility = Visibility.Collapsed;
                          Frame.Navigate(typeof(TrainingModulesPage));
  
                      }
                  }
                  else
                  {
                      MessageDialog msg1 = new MessageDialog("Somthing went wrong.Try again later!");
                      await msg1.ShowAsync();
                      Frame.Navigate(typeof(RegistrationPage));
                  }
              }
              else
              {
                  MessageDialog msg1 = new MessageDialog("You are not connected to internet!");
                  await msg1.ShowAsync();
  
  
        Frame.Navigate(typeof(RegistrationPage));
              }
  
  
  
  
          }
   public Page renderArea { get; set; }
          public string refreshToken { get; set; }
          public string code { get; set; }
  </code></pre>
  
  <p>Here in ContinueWebAuthentication which is triggered after user accepts to let the app get the profile info the value of "code" is not the desired one,In W8.1 app the value of "code" is correct but here it is not.
  Due to this I am unable to get the user profile info </p>