Twitter Authentication

Authentication

To get started with accessing the Twitter API, you should first create a Twitter app. You create (or manage existing) apps at the Twitter Application Management page.

A Twitter app will have a consumer key and consumer secret that identifies your app and is used for security purposes when communicating with the Twitter API. Your app also has an access level that you can change depending on your needs. The access level defines whether your app will have write access or readonly access on behalf of the users that authenticate with your app.

Once you have created your app, there are two ways that you can access data in the Twitter API. Given that you just use the consumer key and consumer secret as described above, your app will communicate with the Twitter API using an app context.

To access the API on behalf of your users (user context), you can specify an access token and access token secret as well.

One last thing worth mentioning is that if you just need to access public data (eg. tweets of a user), an app context may be sufficient.

App context

If you don't need to read or write data on behalf of a specific user, you can use an app context, which is done by specifying the consumer key and consumer secret of your app.

In Skybrud.Social, you can initialize new instances of the TwitterOAuthClient and TwitterService classes with an app context like:

// Initialize a new client
TwitterOAuthClient client = new TwitterOAuthClient {
    ConsumerKey = "your app key",
    ConsumerSecret = "your app secret"
};

// Initialize a new service based on the client
TwitterService service = TwitterService.CreateFromOAuthClient(client);

User context

If you instead need a user context - eg. if you need to post tweets on behalf of the user or access private data - your code could look like:

// Initialize a new client
TwitterOAuthClient client = new TwitterOAuthClient {
    ConsumerKey = "your app key",
    ConsumerSecret = "your app secret",
    Token = "access token",
    TokenSecret = "access token secret"
};

// Initialize a new service based on the client
TwitterService service = TwitterService.CreateFromOAuthClient(client);

If you just need to access the API on behalf of your own account, you can go to the Twitter Application Management page, select your app, and go to the Keys and Access Tokens tab. You can find an access token and access token secret for your own account at the bottom of the page.

To access the API on behalf of your users, you need to set up an authentication page, which lets you obtain an access token and access token secret for each user.