linqtotwitter - grab the saved credentials

天涯浪子 提交于 2019-12-06 17:04:27

问题


i use linqtotwitter library, in my win forms application i can do api calls to twitter, but when i closed my application and reopen, i need again repeat enter pin code///

in twitter programm tweetdesc its need only once enter the pin? How i can do well as in the application itself?

i read:

After authorize, save the credentials associated with that user. Next time the user needs to perform an action with your app, grab the saved credentials and load all 4 credentials into the authorizer. When the authorizer has all 4 credentials, it no longer needs to perform the authorization process and you can perform queries without authorizing

BUT how do thise?


回答1:


You can read credentials after calling CompleteAuthorizeAsync, like this:

        await pinAuth.CompleteAuthorizeAsync(PinTextBox.Text);
        SharedState.Authorizer = pinAuth;

        // This is how you access credentials after authorization.
        // The oauthToken and oauthTokenSecret do not expire.
        // You can use the userID to associate the credentials with the user.
        // You can save credentials any way you want - database, isolated storage, etc. - it's up to you.
        // You can retrieve and load all 4 credentials on subsequent queries to avoid the need to re-authorize.
        // When you've loaded all 4 credentials, LINQ to Twitter will let you make queries without re-authorizing.
        //
        var credentials = pinAuth.CredentialStore;
        string oauthToken = credentials.OAuthToken;
        string oauthTokenSecret = credentials.OAuthTokenSecret;
        string screenName = credentials.ScreenName;
        ulong userID = credentials.UserID;

So, what you need to do is have a way to store user information. For simplicity, I'll assume you're using a database. Your storage could also be a file in any format you choose. Assuming you're using a database, you should have a table that holds user information and you should know who the user is that is using your application. That user has an ID in your table and the table should have fields for oauthToken and oauthTokenSecret. You could also add fields for Twitter's UserID and ScreenName, but they aren't required for OAuth.

Notice that the code above gets a reference to the CredentialStore from the authorizer, pinAuth. This occurs after the call to CompleteAuthorizeAsync because the credentials are not available until after the OAuth process is complete. With the reference to credentials, read the OAuthToken and OAuthToken properties. Then write code to store the OAuthToken and OAuthTokenSecret credentials into the database, associated with the current user.

Now you have credentials stored for that user.

On subsequent queries, make sure that you've loaded the Authorizer with all four credentials: ConsumerKey, ConsumerSecret, OAuthToken, and OAuthTokenSecret. Here's an example:

        string oauthToken = null;
        string oauthTokenSecret = null;

        // read OAuthToken and OAuthTokenSecret from the database table where you previously
        // stored OAuthToken and OAuthTokenSecret for this user. Put the OAuthToken and
        // OAuthTokenSecret into the variables named oauthToken and oauthTokenSecret above.

        pinAuth = new PinAuthorizer
        {
            // Get the ConsumerKey and ConsumerSecret for your app and load them here.
            CredentialStore = new InMemoryCredentialStore
            {
                ConsumerKey = ConfigurationManager.AppSettings["consumerKey"],
                ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"],
                OAuthToken = oauthToken,
                OAuthTokenSecret = oauthTokenSecret
            },
            // Note: GetPin isn't used here because we've broken the authorization
            // process into two parts: begin and complete
            GoToTwitterAuthorization = pageLink => 
                OAuthWebBrowser.Navigate(new Uri(pageLink, UriKind.Absolute))
        };

        if (oauthToken == null)
            await pinAuth.BeginAuthorizeAsync();

Before instantiating the PinAuthorizer, check to see if you have an OAuthToken and OAuthTokenSecret for the current user. If you do, then populate the Authorizer's CredentialStore with them. If not, leave the OAuthToken and OAuthTokenSecret null so that LINQ to Twitter will go through the OAuth process to get the tokens. If you do have OAuthToken and OAuthTokenSecret and you assign them to CredentialStore properties, then LINQ to Twitter will not need to perform the OAuth process and you can use the authorizer to perform queries right away.



来源:https://stackoverflow.com/questions/22542146/linqtotwitter-grab-the-saved-credentials

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!