linqtotwitter - 抢保存的凭据 [英] linqtotwitter - grab the saved credentials

查看:261
本文介绍了linqtotwitter - 抢保存的凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用linqtotwitter库,在我赢窗体应用程序,我可以做API调用Twitter的,但是当我关闭我的应用程序,然后重新打开,我需要再重复输入PIN码///

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///

在微博PROGRAMM tweetdesc其只需要输入一次脚? ?我怎么能在应用程序本身做好

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

我读:

授权后,保存凭据与该用户相关联。下一次用户需要使用您的应用程序执行的操作,抢保存的凭证和所有4个证书加载到授权人。当授权人拥有所有4凭据,它不再需要执行授权程序,你可以不授权执行查询

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

但是,如何thise?

推荐答案

您可以致电CompleteAuthorizeAsync,这样以后读凭据:

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;



所以,你需要做的是有一种方法来存储用户信息。为简单起见,我假设你正在使用的数据库。您的存储也可以在你选择的任何格式的文件。您正在使用的数据库假设,你应该有保存用户信息的表,你应该知道用户是谁正在使用你的应用程序。那用户表有一个ID和表应该有oauthToken和oauthTokenSecret领域。您还可以添加字段Twitter的用户名和屏幕,但他们并不需要的OAuth。

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.

请注意,上面的代码从授权人获取到CredentialStore参考, pinAuth。因为凭据不能使用后才OAuth的过程完成后会出现这种情况调用CompleteAuthorizeAsync后。随着参考凭证,阅读OAuthToken和OAuthToken属性。然后编写代码来存储OAuthToken和OAuthTokenSecret凭据到数据库中,与当前用户相关联。

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.

在随后的查询中,确保加载了与授权者所有四个凭据:ConsumerKey,ConsumerSecret,OAuthToken和OAuthTokenSecret。这里有一个例子:

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();



实例化PinAuthorizer之前,请检查您是否有一个OAuthToken和OAuthTokenSecret为当前用户。如果你这样做,那么填充授权者CredentialStore他们。如果没有,离开OAuthToken和OAuthTokenSecret空,使LINQ到Twitter将通过OAuth的过程中获得的令牌。如果你确实有OAuthToken和OAuthTokenSecret,你将它们分配给CredentialStore属性,然后LINQ到Twitter将不再需要进行OAuth的过程中,你可以使用该授权人执行查询的时候了。

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.

这篇关于linqtotwitter - 抢保存的凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆