void AuthenticateWithBrowser() { var userAuthProvider = new UserAuthentication(_connection); _authProvider = userAuthProvider; userAuthProvider.TokenChanged += userAuthProvider_TokenChanged; OAuthToken authToken = userAuthProvider.GetToken(); string savedToken = authToken.ToJson(); // This can be restored into a UserAuthentication object later to reuse: OAuthToken restoredToken = OAuthToken.FromJson(savedToken); UserAuthentication restoredAuthProvider = new UserAuthentication(_connection, restoredToken); // Now the user will not be prompted when we call GetToken OAuthToken cachedToken = restoredAuthProvider.GetToken(); }
/// <summary> /// Shows how to set up authentication to authenticate the user in an embedded browser form /// and get an OAuth2 token by prompting the user for credentials. /// </summary> private static void AuthenticateWithBrowser() { // The UserAuthentication class will handle the OAuth2 desktop // authentication flow using an embedded WebBrowser form, // cache the returned token for later API usage, and handle token refreshes. var userAuthProvider = new UserAuthentication(_connection); _authProvider = userAuthProvider; // optionally register an event handler to be notified if/when the auth // token changes userAuthProvider.TokenChanged += userAuthProvider_TokenChanged; // Retrieve a token from the server // Note: the RestApi class will call this as needed so it isn't required // to call it before accessing the API. However, manually calling GetToken first // is recommended so the app can more gracefully handle authentication errors OAuthToken authToken = userAuthProvider.GetToken(); // OAuth2 tokens can and should be cached across application uses so users // don't need to grant access every time they run the application. // To do this, call OAuthToken.ToJSon to get a serialized version of // the token that can be used later. Be sure to treat this string as a // user password and store it securely! // Note that this token will potentially be refreshed during API usage // using the OAuth2 token refresh protocol. If that happens, your application // should overwrite the previously saved token with the new token value. // You can register for the TokenChanged event to be notified of any new/changed tokens // or you can call UserAuthentication.GetToken().ToJson() after using the API // to manually retrieve the most current token. string savedToken = authToken.ToJson(); // This can be restored into a UserAuthentication object later to reuse: OAuthToken restoredToken = OAuthToken.FromJson(savedToken); UserAuthentication restoredAuthProvider = new UserAuthentication(_connection, restoredToken); // Now the user will not be prompted when we call GetToken OAuthToken cachedToken = restoredAuthProvider.GetToken(); }