quinta-feira, 16 de julho de 2015

LoginGoogle

Code Acess Google

public String strResult;

       public class GoogleUserInfo
       {
          public string id { get; set; }
          public string email { get; set; }
          public bool verified_email { get; set; }
          public string name { get; set; }
          public string given_name { get; set; }
          public string family_name { get; set; }
          public string link { get; set; }
          public string picture { get; set; }
          public string gender { get; set; }
      }

public void LoginGoogle()
        {
          var auth = new Xamarin.Auth.OAuth2Authenticator(
          clientId: "YOURCLIENTid.apps.googleusercontent.com",
          clientSecret:"YOURSECRET",
           scope: "https://www.googleapis.com/auth/userinfo.email",
          authorizeUrl: new Uri("https://accounts.google.com/o/oauth2/auth"),
          redirectUrl: new Uri("http://lapuinka.blogspot.com.br/2015/07/5-best-android-orms.html"),
          accessTokenUrl: new Uri("https://accounts.google.com/o/oauth2/token"),
          getUsernameAsync: null);
          string access_token;
          string email;

          auth.Completed += (sender, e) =>
          {
            Console.WriteLine(e.IsAuthenticated);
            try
            {
              e.Account.Properties.TryGetValue("access_token", out access_token);
              this.txtMatricula.Text = "Google OK" +access_token;
            }
            catch (System.Exception ex)
            {
           
              access_token = ex.Message;

            }
           
            //get user full information
            getInfo(access_token);

          };
          var intent = auth.GetUI(this);
          StartActivity(intent);
        }
        async void getInfo(string access_token)
        {  
            //do RESP request,by appending token
            string userInfo = await GetDataFromGoogle (access_token );
            if ( userInfo != "Exception" )
            {
                //Deserialize  to objet
              GoogleUserInfo googleIngo = JsonConvert.DeserializeObject<GoogleUserInfo>(userInfo);
              this.txtMatricula.Text = googleIngo.name;
               imageView.SetImageURI(Android.Net.Uri.Parse(googleIngo.picture));
            }
            else
            {
              this.txtMatricula.Text = "exception";
            }
        }

      async   Task<string> GetDataFromGoogle(string accessTokenValue)
      {   
          string  strURL =    "https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + accessTokenValue;  
          WebClient client = new WebClient ();
          try
          {
              strResult=await client.DownloadStringTaskAsync (new Uri(strURL));
              //Console.WriteLine("downloaded");
          }
          catch
          {
              strResult="Exception";
          }
          finally
          {
              client.Dispose ();
              client = null;
          }
          return strResult;
      }
Postar um comentário