コード例 #1
0
        /// <summary>
        /// Creates a TrovoConnection object from an OAuth token.
        /// </summary>
        /// <param name="token">The OAuth token to use</param>
        /// <param name="refreshToken">Whether to refresh the token</param>
        /// <returns>The TrovoConnection object</returns>
        public static async Task <TrovoConnection> ConnectViaOAuthToken(OAuthTokenModel token, bool refreshToken = true)
        {
            Validator.ValidateVariable(token, "token");

            TrovoConnection connection = new TrovoConnection(token);

            if (refreshToken)
            {
                await connection.RefreshOAuthToken();
            }

            return(connection);
        }
コード例 #2
0
        /// <summary>
        /// Creates a TrovoConnection object from an OAuth authentication locally.
        /// </summary>
        /// <param name="clientID">The ID of the client application</param>
        /// <param name="scopes">The authorization scopes to request</param>
        /// <param name="state">The state for authentication check</param>
        /// <param name="forceApprovalPrompt">Whether to force an approval from the user</param>
        /// <param name="oauthListenerURL">The URL to listen for the OAuth successful authentication</param>
        /// <param name="successResponse">The response to send back upon successful authentication</param>
        /// <returns>The TrovoConnection object</returns>
        public static async Task <TrovoConnection> ConnectViaLocalhostOAuthBrowser(string clientID, IEnumerable <OAuthClientScopeEnum> scopes, string state = "abc123", bool forceApprovalPrompt = false, string oauthListenerURL = DEFAULT_OAUTH_LOCALHOST_URL, string successResponse = null)
        {
            Validator.ValidateString(clientID, "clientID");
            Validator.ValidateList(scopes, "scopes");

            LocalOAuthHttpListenerServer oauthServer = new LocalOAuthHttpListenerServer(oauthListenerURL, DEFAULT_ACCESS_TOKEN_URL_PARAMETER, successResponse);

            oauthServer.Start();

            string url = await TrovoConnection.GetAuthorizationCodeURLForOAuthBrowser(clientID, scopes, oauthListenerURL, state, forceApprovalPrompt);

            ProcessStartInfo startInfo = new ProcessStartInfo()
            {
                FileName = url, UseShellExecute = true
            };

            Process.Start(startInfo);

            string accessToken = await oauthServer.WaitForAuthorizationCode();

            oauthServer.Stop();

            if (accessToken != null)
            {
                TrovoConnection connection = new TrovoConnection(new OAuthTokenModel()
                {
                    clientID         = clientID,
                    accessToken      = accessToken,
                    AcquiredDateTime = DateTimeOffset.Now,
                    expiresIn        = int.MaxValue,
                });

                OAuthTokenValidationModel validation = await connection.OAuth.ValidateToken(connection.token);

                connection.token.expiresTimeStamp = validation.expire_ts;

                return(connection);
            }
            return(null);
        }
コード例 #3
0
        /// <summary>
        /// Generates the OAuth authorization URL to use for authentication.
        /// </summary>
        /// <param name="clientID">The ID of the client application</param>
        /// <param name="scopes">The authorization scopes to request</param>
        /// <param name="redirectUri">The redirect URL for the client application</param>
        /// <param name="state">The state for authentication check</param>
        /// <param name="forceApprovalPrompt">Whether to force an approval from the user</param>
        /// <returns>The authorization URL</returns>
        public static async Task <string> GetAuthorizationCodeURLForOAuthBrowser(string clientID, IEnumerable <OAuthClientScopeEnum> scopes, string redirectUri, string state = "abc123", bool forceApprovalPrompt = false)
        {
            Validator.ValidateString(clientID, "clientID");
            Validator.ValidateList(scopes, "scopes");
            Validator.ValidateString(redirectUri, "redirectUri");
            Dictionary <string, string> parameters = new Dictionary <string, string>()
            {
                { "client_id", clientID },
                { "response_type", "token" },
                { "scope", TrovoConnection.ConvertClientScopesToString(scopes) },
                { "redirect_uri", redirectUri },
                { "state", state },
            };

            if (forceApprovalPrompt)
            {
                parameters.Add("force_verify", "force");
            }

            FormUrlEncodedContent content = new FormUrlEncodedContent(parameters.AsEnumerable());

            return(OAuthService.OAuthBaseAddress + "?" + await content.ReadAsStringAsync());
        }