Storing Twitter access token using Twitterizer

2019-04-17 11:23发布

问题:

I'm trying to store Twitter access token to my database for a specific user, so he doesn't need to approve the application every time he wants to post a new tweet.

I can easily get user access token using Twitterizer, and what I get is OAuthTokenResponse object. It consists of several properties, but for each request (eg. posting a tweet) it asks for OAuthTokenResponse object, so it looks like its not enough to store the Token property only?

What's the best way to store this AccessToken? Thanks!

回答1:

You should store the 4 properties returned by the OAuthUtility.GetAccessToken method in your database. All authorized API calls require an OAuthTokens object. All you need to do is instantiate a new object, set the properties to the values you have (consumer token and access token), then pass your instance to the method.

For example:

OAuthTokens tokens = new OAuthTokens();
tokens.AccessToken = "XXX";
tokens.AccessTokenSecret = "XXX";
tokens.ConsumerKey = "XXX";
tokens.ConsumerSecret = "XXX";

TwitterResponse<TwitterUser> showUserResponse = TwitterUser.Show(tokens, "twit_er_izer");

Or, if you're using VB.NET:

Dim tokens As New OAuthTokens()
tokens.AccessToken = "XXX"
tokens.AccessTokenSecret = "XXX"
tokens.ConsumerKey = "XXX"
tokens.ConsumerSecret = "XXX"

Dim showUserResponse As TwitterResponse(Of TwitterUser) = TwitterUser.Show(tokens, "twit_er_izer")