Google+ API: How can I use RefreshTokens to avoid requesting access every time my app launches?

前端 未结 5 1447
不知归路
不知归路 2020-12-02 08:45

I\'m trying to use the Google+ API to access info for the authenticated user. I\'ve copied some code from one of the samples, which works fine (below), however I\'m having t

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-02 09:30

    Here is an example. Make sure you add a string setting called RefreshToken and reference System.Security or find another way to safely store the refresh token.

        private static byte[] aditionalEntropy = { 1, 2, 3, 4, 5 };
    
        private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
        {
            // Get the auth URL:
            IAuthorizationState state = new AuthorizationState(new[] { PlusService.Scopes.PlusMe.GetStringValue() });
            state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
    
            string refreshToken = LoadRefreshToken();
            if (!String.IsNullOrWhiteSpace(refreshToken))
            {
                state.RefreshToken = refreshToken;
    
                if (arg.RefreshToken(state))
                    return state;
            }
    
            Uri authUri = arg.RequestUserAuthorization(state);
    
            // Request authorization from the user (by opening a browser window):
            Process.Start(authUri.ToString());
            Console.Write("  Authorization Code: ");
            string authCode = Console.ReadLine();
            Console.WriteLine();
    
            // Retrieve the access token by using the authorization code:
            var result = arg.ProcessUserAuthorization(authCode, state);
    
            StoreRefreshToken(state);
            return result;
        }
    
        private static string LoadRefreshToken()
        {
            return Encoding.Unicode.GetString(ProtectedData.Unprotect(Convert.FromBase64String(Properties.Settings.Default.RefreshToken), aditionalEntropy, DataProtectionScope.CurrentUser));
        }
    
        private static void StoreRefreshToken(IAuthorizationState state)
        {
            Properties.Settings.Default.RefreshToken = Convert.ToBase64String(ProtectedData.Protect(Encoding.Unicode.GetBytes(state.RefreshToken), aditionalEntropy, DataProtectionScope.CurrentUser));
            Properties.Settings.Default.Save();
        }
    

提交回复
热议问题