Exemple #1
0
        private static async Task AuthorizeInBrowser(OAuthDesktopMobileAuthCodeGrant auth)
        {
            var browserWindow = new BrowserWindow(auth.GetAuthorizationEndpoint(), auth.RedirectionUri.AbsolutePath);

            browserWindow.Show();

            var redirectUri = await browserWindow.GetRedirectUri();

            await auth.RequestAccessAndRefreshTokensAsync(redirectUri);
        }
Exemple #2
0
        private static Authentication AuthenticateWithOAuth()
        {
            var apiEnvironment =
                ConfigurationManager.AppSettings["BingAdsEnvironment"] == ApiEnvironment.Sandbox.ToString() ?
                ApiEnvironment.Sandbox : ApiEnvironment.Production;
            var oAuthDesktopMobileAuthCodeGrant = new OAuthDesktopMobileAuthCodeGrant(
                Settings.Default["ClientId"].ToString(),
                apiEnvironment
                );

            // It is recommended that you specify a non guessable 'state' request parameter to help prevent
            // cross site request forgery (CSRF).
            oAuthDesktopMobileAuthCodeGrant.State = ClientState;

            string refreshToken;

            // If you have previously securely stored a refresh token, try to use it.
            if (GetRefreshToken(out refreshToken))
            {
                AuthorizeWithRefreshTokenAsync(oAuthDesktopMobileAuthCodeGrant, refreshToken).Wait();
            }
            else
            {
                // You must request user consent at least once through a web browser control.
                Console.WriteLine(string.Format(
                                      "Open a new web browser and navigate to {0}\n\n" +
                                      "Grant consent in the web browser for the application to access " +
                                      "your advertising accounts, and then enter the response URI that includes " +
                                      "the authorization 'code' parameter: \n", oAuthDesktopMobileAuthCodeGrant.GetAuthorizationEndpoint())
                                  );

                // After consent has been granted, read the reponse URI that should contain the authorization code.
                var responseUri = new Uri(Console.ReadLine());

                if (oAuthDesktopMobileAuthCodeGrant.State != ClientState)
                {
                    throw new HttpRequestException("The OAuth response state does not match the client request state.");
                }

                // Request access and refresh tokens.
                oAuthDesktopMobileAuthCodeGrant.RequestAccessAndRefreshTokensAsync(responseUri).Wait();

                // Store the refresh token for future use as needed.
                SaveRefreshToken(oAuthDesktopMobileAuthCodeGrant.OAuthTokens.RefreshToken);
            }

            // It is important to save the most recent refresh token whenever new OAuth tokens are received.
            // You will want to subscribe to the NewOAuthTokensReceived event handler.
            // Each instance of ServiceClient<TService>, BulkServiceManager, or ReportingServiceManager
            // will refresh your access token automatically if they detect the AuthenticationTokenExpired (109) error code.
            oAuthDesktopMobileAuthCodeGrant.NewOAuthTokensReceived +=
                (sender, tokens) => SaveRefreshToken(tokens.NewRefreshToken);

            return(oAuthDesktopMobileAuthCodeGrant);
        }
        private static async Task AuthorizeInBrowser(OAuthDesktopMobileAuthCodeGrant authentication)
        {
            var browserWindow = new BrowserWindow(authentication.GetAuthorizationEndpoint(), authentication.RedirectionUri.AbsolutePath);

            browserWindow.Show();

            var redirectUri = await browserWindow.GetRedirectUri();

            if (authentication.State != ClientState)
            {
                throw new HttpRequestException("The OAuth response state does not match the client request state.");
            }

            await authentication.RequestAccessAndRefreshTokensAsync(redirectUri);
        }
Exemple #4
0
        private static Authentication AuthenticateWithOAuth()
        {
            var oAuthDesktopMobileAuthCodeGrant = new OAuthDesktopMobileAuthCodeGrant(Settings.Default["ClientId"].ToString());

            // It is recommended that you specify a non guessable 'state' request parameter to help prevent
            // cross site request forgery (CSRF).
            oAuthDesktopMobileAuthCodeGrant.State = ClientState;

            string refreshToken;

            // If you have previously securely stored a refresh token, try to use it.
            if (GetRefreshToken(out refreshToken))
            {
                AuthorizeWithRefreshTokenAsync(oAuthDesktopMobileAuthCodeGrant, refreshToken).Wait();
            }
            else
            {
                // You must request user consent at least once through a web browser control.
                // Call the GetAuthorizationEndpoint method of the OAuthDesktopMobileAuthCodeGrant instance that you created above.
                Console.WriteLine(string.Format(
                                      "The Bing Ads user must provide consent for your application to access their Bing Ads accounts.\n" +
                                      "Open a new web browser and navigate to {0}.\n\n" +
                                      "After the user has granted consent in the web browser for the application to access their Bing Ads accounts, " +
                                      "please enter the response URI that includes the authorization 'code' parameter: \n", oAuthDesktopMobileAuthCodeGrant.GetAuthorizationEndpoint()));

                // Request access and refresh tokens using the URI that you provided manually during program execution.
                var responseUri = new Uri(Console.ReadLine());

                if (oAuthDesktopMobileAuthCodeGrant.State != ClientState)
                {
                    throw new HttpRequestException("The OAuth response state does not match the client request state.");
                }

                oAuthDesktopMobileAuthCodeGrant.RequestAccessAndRefreshTokensAsync(responseUri).Wait();
            }

            // It is important to save the most recent refresh token whenever new OAuth tokens are received.
            // You will want to subscribe to the NewOAuthTokensReceived event handler.
            // When calling Bing Ads services with ServiceClient<TService>, BulkServiceManager, or ReportingServiceManager,
            // each instance will refresh your access token automatically if they detect the AuthenticationTokenExpired (109) error code.
            oAuthDesktopMobileAuthCodeGrant.NewOAuthTokensReceived +=
                (sender, tokens) => SaveRefreshToken(tokens.NewRefreshToken);

            return(oAuthDesktopMobileAuthCodeGrant);
        }