/// <summary>
        /// Initializes a new instance of the <see cref="Salesforce.SalesforceClient"/> class.
        /// </summary>
        /// <param name="appKey">App key.</param>
        /// <param name="callbackUri">Callback URI.</param>
        public SalesforceClient(String clientId, String clientSecret, Uri redirectUrl)
        {
            ClientId            = clientId;
            ClientSecret        = clientSecret;
            MainThreadScheduler = TaskScheduler.FromCurrentSynchronizationContext();
            Scheduler           = TaskScheduler.Default;

                        #if PLATFORM_IOS
            Adapter = new UIKitPlatformAdapter();
                        #elif __ANDROID__
            Adapter = new AndroidPlatformAdapter();
                        #endif

            var users = LoadUsers().Where(u => !u.RequiresReauthentication).ToArray();

            if (users.Count() > 0)
            {
                CurrentUser = users.First();

                Debug.WriteLine(CurrentUser);

                foreach (var p in CurrentUser.Properties)
                {
                    Debug.WriteLine("{0}\t{1}", p.Key, p.Value);
                }
            }

            Authenticator = new OAuth2Authenticator(
                clientId: clientId,
                clientSecret: ClientSecret,
                scope: "api refresh_token",
                authorizeUrl: new Uri(AuthPath),
                redirectUrl: redirectUrl,
                accessTokenUrl: new Uri(TokenPath),
                getUsernameAsync: new GetUsernameAsyncFunc((dict) => {
                var client = new WebClient();
                client.Headers["Authorization"] = "Bearer " + dict["access_token"];

                var results    = client.DownloadString(dict["id"]);
                var resultVals = JsonValue.Parse(results);

                return(Task.Factory.StartNew(() => {
                    return (String)resultVals["username"];
                }));
            })
                );

            if (CurrentUser == null || CurrentUser.RequiresReauthentication)
            {
                Authenticator.AccessTokenUrl = null;
            }

            Adapter.Authenticator = Authenticator;

            Authenticator.Completed += OnCompleted;
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Salesforce.SalesforceClient"/> class.
        /// </summary>
        /// <param name="appKey">App key.</param>
        /// <param name="callbackUri">Callback URI.</param>
        public SalesforceClient(String clientId, String clientSecret, Uri redirectUrl)
        {
            ClientId            = clientId;
            ClientSecret        = clientSecret;
            MainThreadScheduler = TaskScheduler.FromCurrentSynchronizationContext();
            Scheduler           = TaskScheduler.Default;

#if PLATFORM_IOS
            Adapter = new UIKitPlatformAdapter();
#elif __ANDROID__
            Adapter = new AndroidPlatformAdapter();
#endif

            var users = LoadUsers().Where(u => !u.RequiresReauthentication).ToArray();

            if (users.Count() > 0)
            {
                CurrentUser = users.First();

                Debug.WriteLine(CurrentUser);

                foreach (var p in CurrentUser.Properties)
                {
                    Debug.WriteLine("{0}\t{1}", p.Key, p.Value);
                }
            }

            //
            // [email protected] 2015.01.08
            //
            // Note that here the original authors used the OAuth2Authenticator constructor intended for the Web-Server Flow;
            // however, they then set AccessTokenUrl to null to force the authenticator to run the User-Agent Flow.
            // I think they did this to load the ClientSecret into the authenticator for use with refresh (the ClientSecret
            // isn't needed for the User-Agent Flow).
            //
            Authenticator = new OAuth2Authenticator
                            (
                clientId: clientId,
                clientSecret: ClientSecret,
                scope: "api refresh_token",
                authorizeUrl: new Uri(AuthPath),
                redirectUrl: redirectUrl,
                accessTokenUrl: new Uri(TokenPath),
                getUsernameAsync: new GetUsernameAsyncFunc(HandleGetUsernameAsyncFunc)
                            );

            if (CurrentUser == null || CurrentUser.RequiresReauthentication)
            {
                Authenticator.AccessTokenUrl = null;
            }

            Adapter.Authenticator = Authenticator;

            Authenticator.Completed += OnAuthenticationCompleted;
        }