AcquireAccessToken() public method

Acquire an access token, from the given URI, using the given HTTP method.

To use this method, you must first set the oauth_token to the value of the request token. Eg, oauth["token"] = "whatever".

According to the OAuth spec, you need to do this only ONCE per application. In other words, the first time the application is run. The normal oauth workflow is: (1) get a request token, (2) use that to acquire an access token (which requires explicit user approval), then (3) using that access token, invoke protected services. The first two steps need to be done only once per application.

For Twitter, at least, you can cache the access tokens indefinitely; Twitter says they never expire. However, other oauth services may not do the same. Also: the user may at any time revoke his authorization for your app, in which case you need to perform the first 2 steps again.

public AcquireAccessToken ( string uri, string method, string pin, string user_agent ) : OAuthResponse
uri string
method string
pin string
user_agent string
return OAuthResponse
Beispiel #1
0
        public string AuthorizeApp(string pin)
        {
            OAuthResponse accessToken = OAuth.AcquireAccessToken("http://api.discogs.com/oauth/access_token", "POST", pin, _UserAgent);
            var           search      = "http://api.discogs.com/oauth/identity";
            var           authHeader  = OAuth.GenerateAuthzHeader(search, "GET");

            return(authHeader);
        }
        public void AcquireAccessToken()
        {
            string code     = (string)Settings.Default["EtsyVerificationCode"];
            var    response = oAuth.AcquireAccessToken(ACCESS_TOKEN_URI, "GET", code);

            Settings.Default.EtsyAccessToken       = response["oauth_token"];
            Settings.Default.EtsyAccessTokenSecret = response["oauth_token_secret"];
            Settings.Default.Save();
            oAuth["token"]        = response["oauth_token"];
            oAuth["token_secret"] = response["oauth_token_secret"];
        }
        public async Task<bool?> Init(MetroWindow win)
        {
            try
            {
                parentWindow = win as MainWindow;

                authMgr = new Manager();
                authMgr["consumer_key"] = ConsumerKey;
                authMgr["consumer_secret"] = ConsumerSecret;
                authMgr.AcquireRequestToken(RequestTokenURL, "POST");
                MetroDialogSettings settings = new MetroDialogSettings()
                {
                    AnimateHide = true,
                    AnimateShow = true,
                    AffirmativeButtonText = "확인",
                    NegativeButtonText = "종료"
                };

                MessageDialogResult result = await DialogManager.ShowMessageAsync(parentWindow, "인증", "인증을 위해 웹브라우저를 통해 트위터에 로그인합니다.",
                    MessageDialogStyle.AffirmativeAndNegative, settings);

                if (result == MessageDialogResult.Affirmative)
                {
                    System.Diagnostics.Process.Start($"{RequestPinURL}{authMgr["token"]}");
                    settings = new MetroDialogSettings()
                    {
                        AnimateHide = true,
                        AnimateShow = true,
                        AffirmativeButtonText = "인증",
                        NegativeButtonText = "취소 및 종료"
                    };
                    
                    string pin = await DialogManager.ShowInputAsync(parentWindow, "PIN", "웹브라우저 상에 표시된 PIN을 입력합니다.", settings);

                    try
                    {
                        authMgr.AcquireAccessToken(AccessTokenURL, "POST", pin);
                    }
                    catch(Exception ex)
                    {
                        parentWindow.Close();
                    }

                    IsInit = true;

                    // Get Screen Name
                    dynamic obj = await GetJson("https://api.twitter.com/1.1/account/settings.json", "GET");
                    string name = obj.screen_name;

                    // GetProfile
                    dynamic json = await GetJson($"https://api.twitter.com/1.1/users/show.json?screen_name={name}", "GET");
                    parentWindow.UserNameBlock.Text = json.name;
                    parentWindow.ScreenNameBlock.Text = $"@{json.screen_name}";
                    parentWindow.ProfileDescBlock.Text = json.description;
                    string imageUrl = json.profile_image_url;
                    ProfileImage = new BitmapImage();
                    ProfileImage.BeginInit();
                    ProfileImage.UriSource = new Uri(imageUrl.Replace("_normal", ""));
                    ProfileImage.DownloadCompleted +=
                        (s, ev) => OnPropertyChanged("ProfileImage");
                    ProfileImage.EndInit();
                }
                else
                {
                    parentWindow.Close();
                }

                return IsInit;
            }
            catch (Exception e)
            {
                await DialogManager.ShowMessageAsync(parentWindow, "Error", $"= Message{e.Message}\n\n= Stack Trace\n{e.StackTrace}");
                IsInit = false;
                return false;
            }
        }