Exemplo n.º 1
0
 /// <summary>
 /// Methode qui permet de rafraichir le Token depuis l'access Token
 /// </summary>
 /// <param name="refresh_token"></param>
 /// <returns></returns>
 public async Task<bool> RefreshToken(string refresh_token)
 {
     string postData = string.Format("client_id={0}&client_secret={1}&refresh_token={2}&grant_type=refresh_token", ClientID, ClientSecret, refresh_token);
     StringContent content = new StringContent(postData, Encoding.UTF8, "application/x-www-form-urlencoded");
     Stream result = await Post("https://accounts.google.com/o/oauth2/token", content, false);
     if (result != null)
     {
         DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(Token));
         AccessToken = (Token)deserializer.ReadObject(result);
         AccessToken.RefreshToken = refresh_token;
         FileManager.WriteFileAsync(FILE_NAME, CryptographicManager.Encrypt(AccessToken.RefreshToken, KEY));
         return true;
     }
     return false;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Methode qui supprime le token
        /// </summary>
        public async void CleanToken()
        {
            bool tok=false;
            try
            {
                string encryptedText = await FileManager.ReadFile(FILE_NAME);
                string decryptedText = CryptographicManager.Decrypt(encryptedText, KEY);
                if (!String.IsNullOrEmpty(encryptedText))
                    tok = true;
            }
            catch (Exception)
            {
            }

            if (tok)
            {
                FileManager.RemoveFileAsync(FILE_NAME);
                AccessToken = null;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Methode qui permet de récupérer un nouveau TOKEN
        /// </summary>
        /// <returns></returns>
        public async Task<bool> LoadToken()
        {
            //Step 1. Create a Request and Callback Uri for the Authentication operation
            Uri requestUri = new Uri(string.Format("{0}?scope={1}&redirect_uri={2}&response_type={3}&client_id={4}",googleUri, Scope, redirect_uri, response_type, ClientID), UriKind.RelativeOrAbsolute);
            Uri callbackUri = new Uri(redirect_uri, UriKind.RelativeOrAbsolute);

            //Step 2. Initialize a Authentication operation using WebAuthenticationBroker
            WebAuthenticationResult webResult = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None,requestUri, callbackUri);

            switch (webResult.ResponseStatus)
            {
                case WebAuthenticationStatus.ErrorHttp:
                    GoogleErrorOccured(webResult.ResponseData);
                    break;

                case WebAuthenticationStatus.Success:
                    string code = webResult.ResponseData.Split(new string[] { "code=" }, StringSplitOptions.RemoveEmptyEntries)[1];
                    string postData = string.Format("client_id={0}&client_secret={1}&code={2}&redirect_uri={3}&grant_type=authorization_code", ClientID, ClientSecret, code, redirect_uri);
                    StringContent content = new StringContent(postData, Encoding.UTF8, "application/x-www-form-urlencoded");
                    Stream result = await Post("https://accounts.google.com/o/oauth2/token", content, false);

                    if (result != null)
                    {
                        DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(Token));
                        byte[] arrayMessage = new byte[result.Length];
                        result.Read(arrayMessage, 0, arrayMessage.Length);
                        var message = Encoding.UTF8.GetString(arrayMessage, 0, arrayMessage.Length);
                        System.Diagnostics.Debug.WriteLine(message);
                        result.Position = 0;
                        AccessToken = (Token)deserializer.ReadObject(result);
                        FileManager.WriteFileAsync(FILE_NAME, CryptographicManager.Encrypt(AccessToken.RefreshToken, KEY));
                        return true;
                    }
                    break;

                case WebAuthenticationStatus.UserCancel:
                    //The User selected to cancel the Authentication process
                    break;

                default:
                    break;

            }
            return false;
        }