private void DoAuth2Configuration(DfaAppConfig config)
        {
            // Since we use this page for OAuth callback also, we set the callback
              // url as the current page. For a non-web application, this will be null.
              config.OAuth2RedirectUri = Request.Url.GetLeftPart(UriPartial.Path);

              // Create an OAuth2 object for handling OAuth2 flow.
              OAuth2ProviderForApplications oAuth = new OAuth2ProviderForApplications(config);

              if (Request.Params["state"] == null) {
            // This is the first time this page is being loaded.
            // Set the state variable to any value that helps you recognize
            // when this url will be called by the OAuth2 server.
            oAuth.State = "callback";

            // Create an authorization url and redirect the user to that page.
            Response.Redirect(oAuth.GetAuthorizationUrl());
              } else if (Request.Params["state"] == "callback") {
            // This page was loaded because OAuth server did a callback.
            // Retrieve the authorization code from the url and use it to fetch
            // the access token. This call will also fetch the refresh token if
            // your mode is offline.
            oAuth.FetchAccessAndRefreshTokens(Request.Params["code"]);

            // Save the OAuth2 provider for future use. If you wish to save only
            // the values and restore the object later, then save
            // oAuth.RefreshToken, oAuth.AccessToken, oAuth.UpdatedOn and
            // oAuth.ExpiresIn.
            //
            // You can later restore the values as
            // DfaUser user = new AdWordsUser();
            // user.Config.OAuth2Mode = OAuth2Flow.APPLICATION;
            // OAuth2ProviderForApplications oAuth =
            //     (user.OAuthProvider as OAuth2ProviderForApplications);
            // oAuth.RefreshToken = xxx;
            // oAuth.AccessToken = xxx;
            // oAuth.UpdatedOn = xxx;
            // oAuth.ExpiresIn = xxx;
            //
            // Note that only oAuth.RefreshToken is mandatory. If you leave
            // oAuth.AccessToken as empty, or if oAuth.UpdatedOn + oAuth.ExpiresIn
            // is in the past, the access token will be refreshed by the library.
            // You can listen to this event as
            //
            // oAuth.OnOAuthTokensObtained += delegate(AdsOAuthProvider provider) {
            //    OAuth2ProviderForApplications oAuth =
            //        (provider as OAuth2ProviderForApplications);
            //    // Save oAuth.RefreshToken, oAuth.AccessToken, oAuth.UpdatedOn and
            //    // oAuth.ExpiresIn.
            //};
            Session["OAuthProvider"] = oAuth;
            // Redirect the user to the main page.
            Response.Redirect("Default.aspx");
              } else {
            throw new Exception("Unknown state for OAuth callback.");
              }
        }
    public void Init() {
      campaignId = utils.CreateSearchCampaign(user, BiddingStrategyType.MANUAL_CPC);
      adGroupId1 = utils.CreateAdGroup(user, campaignId);
      adGroupId2 = utils.CreateAdGroup(user, campaignId);

      // Load defaults from config file.
      AdWordsAppConfig appConfig = new AdWordsAppConfig();
      appConfig.OAuth2RefreshToken = appConfig.GMBOAuth2RefreshToken;

      AdsOAuthProviderForApplications oAuth2Provider = new OAuth2ProviderForApplications(appConfig);
      oAuth2Provider.RefreshAccessToken();
    }
    public void TestPropertySettersAndGetters() {
      provider = new OAuth2ProviderForApplications(appConfig);

      provider.ClientId = TEST_CLIENT_ID;
      Assert.AreEqual(provider.ClientId, TEST_CLIENT_ID);

      provider.ClientSecret = TEST_CLIENT_SECRET;
      Assert.AreEqual(provider.ClientSecret, TEST_CLIENT_SECRET);

      provider.AccessToken = TEST_ACCESS_TOKEN;
      Assert.AreEqual(provider.AccessToken, TEST_ACCESS_TOKEN);

      provider.RefreshToken = TEST_REFRESH_TOKEN;
      Assert.AreEqual(provider.RefreshToken, TEST_REFRESH_TOKEN);

      provider.Scope = TEST_SCOPE;
      Assert.AreEqual(provider.Scope, TEST_SCOPE);

      provider.State = TEST_STATE;
      Assert.AreEqual(provider.State, TEST_STATE);

      provider.IsOffline = TEST_OFFLINE;
      Assert.AreEqual(provider.IsOffline, TEST_OFFLINE);

      provider.OnOAuthTokensObtained = TEST_CALLBACK;
      Assert.AreEqual(provider.OnOAuthTokensObtained, TEST_CALLBACK);

      provider.IsOffline = TEST_OFFLINE;
      Assert.AreEqual(provider.IsOffline, TEST_OFFLINE);

      provider.RedirectUri = TEST_REDIRECT_URI;
      Assert.AreEqual(provider.RedirectUri, TEST_REDIRECT_URI);
    }
 public void TestGetAuthHeader() {
   provider = new OAuth2ProviderForApplications(appConfig);
   Assert.AreEqual(AUTHORIZATION_HEADER, provider.GetAuthHeader());
 }
    public void TestGetAuthorizationUrl() {
      provider = new OAuth2ProviderForApplications(appConfig);

      TestUtils.ValidateRequiredParameters(provider, new string[] {"ClientId", "Scope"},
          delegate() {
            provider.GetAuthorizationUrl();
          }
      );

      Assert.AreEqual(AUTHORIZATION_URL, provider.GetAuthorizationUrl());
    }
 public void TestConstructor() {
   provider = new OAuth2ProviderForApplications(appConfig);
   Assert.AreEqual(provider.ClientId, appConfig.OAuth2ClientId);
   Assert.AreEqual(provider.ClientSecret, appConfig.OAuth2ClientSecret);
   Assert.AreEqual(provider.AccessToken, appConfig.OAuth2AccessToken);
   Assert.AreEqual(provider.RefreshToken, appConfig.OAuth2RefreshToken);
   Assert.AreEqual(provider.Scope, appConfig.OAuth2Scope);
   Assert.AreEqual(provider.RedirectUri, appConfig.OAuth2RedirectUri);
 }
    public void Init() {
      tblSettings = new Hashtable();

      tblSettings.Add("OAuth2ClientId", "OAuth2ClientId");
      tblSettings.Add("OAuth2ClientSecret", "OAuth2ClientSecret");
      tblSettings.Add("OAuth2AccessToken", "OAuth2AccessToken");
      tblSettings.Add("OAuth2RefreshToken", "OAuth2RefreshToken");
      tblSettings.Add("OAuth2Scope", "OAuth2Scope");
      tblSettings.Add("OAuth2RedirectUri", "OAuth2RedirectUri");

      appConfig = new MockAppConfig();
      appConfig.MockReadSettings(tblSettings);
      provider = new OAuth2ProviderForApplications(appConfig);
      oauth2RequestInterceptor.Intercept = true;
    }
Ejemplo n.º 8
0
    /// <summary>
    /// Initializes a new instance of the <see cref="LoginForm"/> class.
    /// </summary>
    /// <param name="clientId">The OAuth2 client identifier.</param>
    /// <param name="clientSecret">The OAuth2 client secret.</param>
    /// <param name="scope">The OAuth2 scope.</param>
    public LoginForm(string clientId, string clientSecret, string scope) {
      InitializeComponent();
      startLocalServer();

      appConfig.OAuth2RedirectUri = LOCALHOST_ADDRESS;
      appConfig.OAuth2ClientId = clientId;
      appConfig.OAuth2ClientSecret = clientSecret;
      appConfig.OAuth2Scope = scope;

      oAuth2 = new OAuth2ProviderForApplications(appConfig);

      // Get the authorization url and open a browser.
      string authorizationUrl = oAuth2.GetAuthorizationUrl();
      loginBrowser.Navigate(authorizationUrl);
    }