コード例 #1
0
        /// <summary>
        /// Creates a YouTubeLiveConnection object from an OAuth authentication locally.
        /// </summary>
        /// <param name="clientID">The ID of the client application</param>
        /// <param name="clientSecret">The secret of the client application</param>
        /// <param name="scopes">The authorization scopes to request</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 YouTubeLiveConnection object</returns>
        public static async Task <YouTubeConnection> ConnectViaLocalhostOAuthBrowser(string clientID, string clientSecret, IEnumerable <OAuthClientScopeEnum> scopes, 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_AUTHORIZATION_CODE_URL_PARAMETER, successResponse);

            oauthServer.Start();

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

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

            Process.Start(startInfo);

            string authorizationCode = await oauthServer.WaitForAuthorizationCode(secondsToWait : 60);

            oauthServer.Stop();

            if (authorizationCode != null)
            {
                return(await YouTubeConnection.ConnectViaAuthorizationCode(clientID, clientSecret, authorizationCode, redirectUrl : oauthListenerURL));
            }
            return(null);
        }
コード例 #2
0
        protected virtual async Task <string> ConnectViaOAuthRedirect(string oauthPageURL, string listeningAddress, int secondsToWait = 30)
        {
            LocalOAuthHttpListenerServer oauthServer = new LocalOAuthHttpListenerServer(listeningAddress, OAuthExternalServiceBase.DEFAULT_AUTHORIZATION_CODE_URL_PARAMETER, successResponse: OAuthExternalServiceBase.LoginRedirectPageHTML);

            oauthServer.Start();

            ProcessHelper.LaunchLink(oauthPageURL);

            string authorizationCode = await oauthServer.WaitForAuthorizationCode(secondsToWait);

            oauthServer.Stop();

            return(authorizationCode);
        }
コード例 #3
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);
        }