Esempio n. 1
0
        /// <summary>
        /// Initializes the service before MakeApiCall.
        /// </summary>
        /// <param name="methodName">Name of the method.</param>
        /// <param name="parameters">The method parameters.</param>
        protected override void InitForCall(string methodName, object[] parameters)
        {
            DfaAppConfig config      = this.User.Config as DfaAppConfig;
            string       oAuthHeader = null;

            if (this.GetType().Name == "LoginRemoteService")
            {
                // The choice of OAuth comes only when calling LoginRemoteService.
                // All other services will still use the login token.
                if (config.AuthorizationMethod == DfaAuthorizationMethod.OAuth2)
                {
                    if (this.User.OAuthProvider != null)
                    {
                        oAuthHeader = this.User.OAuthProvider.GetAuthHeader();
                    }
                    else
                    {
                        throw new DfaApiException(null, DfaErrorMessages.OAuthProviderCannotBeNull);
                    }
                }
            }
            else
            {
                if (this.Token == null)
                {
                    this.Token = LoginUtil.GetAuthenticationToken(this.User, this.Signature.Version);
                }
            }

            ContextStore.AddKey("OAuthHeader", oAuthHeader);
            ContextStore.AddKey("RequestHeader", requestHeader);
            ContextStore.AddKey("Token", Token);

            base.InitForCall(methodName, parameters);
        }
Esempio n. 2
0
        /// <summary>
        /// Gets the request header.
        /// </summary>
        /// <returns>The request header.</returns>
        private RequestHeader GetRequestHeader()
        {
            DfaAppConfig  config    = (DfaAppConfig)base.Config;
            RequestHeader reqHeader = new RequestHeader();

            reqHeader.ApplicationName = config.GetUserAgent();
            return(reqHeader);
        }
        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.");
              }
        }
 /// <summary>
 /// Handles the Load event of the Page control.
 /// </summary>
 /// <param name="sender">The source of the event.</param>
 /// <param name="e">The <see cref="System.EventArgs"/> instance containing
 /// the event data.</param>
 protected void Page_Load(object sender, EventArgs e) {
   // Create an DfaAppConfig object with the default settings in
   // App.config.
   DfaAppConfig config = new DfaAppConfig();
   if (config.AuthorizationMethod == DfaAuthorizationMethod.OAuth2) {
     if (config.OAuth2Mode == OAuth2Flow.APPLICATION &&
         string.IsNullOrEmpty(config.OAuth2RefreshToken)) {
       DoAuth2Configuration(config);
     }
   }
 }
Esempio n. 5
0
        /// <summary>
        /// Create a service object.
        /// </summary>
        /// <param name="signature">Signature of the service being created.</param>
        /// <param name="user">The user for which the service is being created.
        /// <param name="serverUrl">The server to which the API calls should be
        /// made.</param>
        /// </param>
        /// <returns>An object of the desired service type.</returns>
        public override AdsClient CreateService(ServiceSignature signature, AdsUser user,
                                                Uri serverUrl)
        {
            DfaAppConfig config = (DfaAppConfig)base.Config;

            if (serverUrl == null)
            {
                serverUrl = new Uri(config.DfaApiServer);
            }

            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            CheckServicePreconditions(signature);

            DfaServiceSignature dfaapiSignature = signature as DfaServiceSignature;

            AdsClient service = (AdsClient)Activator.CreateInstance(dfaapiSignature.ServiceType);

            if (config.Proxy != null)
            {
                service.Proxy = config.Proxy;
            }

            service.Timeout = config.Timeout;
            service.Url     = string.Format("{0}{1}/api/dfa-api/{2}",
                                            serverUrl, dfaapiSignature.Version, dfaapiSignature.ServiceEndpoint);
            service.UserAgent = config.GetUserAgent();

            service.User      = user;
            service.Signature = signature;

            service.GetType().GetProperty("RequestHeader").SetValue(service,
                                                                    GetRequestHeader(), null);
            SetRequestHeaderNameSpace(signature as DfaServiceSignature, service);

            return(service);
        }