DEV Community

Adam
Adam

Posted on • Edited on • Originally published at adamstorr.azurewebsites.net

Getting Started with Twitter API using SocialOpinion

Introduction

I've been what you could class as an "Integrations Developer" for over 10 years now. I love spelunking around APIs whether they are RESTful or package based. One of the APIs which has always interested me was the Twitter API however previous attempts to investigate it proved tricky and unfortunately I didn't have the time to get past that. After discovering recently the SocialOpinion API by Jamie Maguire (https://github.com/jamiemaguiredotnet/SocialOpinion-Public) I thought I'd give it another go.

The initial interesting part for me wasn't around the tweet based APIs but around the user management side; user profile, followers, following, mutes, blocks etc. and when I saw that Jamie had implemented the newer v2 end points I thought I'd give the Twitter API another go but through this SDK package instead of calling it directly.

Twitter Developer Account

First off you need to make sure you have a Twitter Developer Account. I signed up for one years ago and to get access to the API I had to register an "application" which allowed for access and consumer secrets/tokens. If you don't have a developer account you will need to register for one to use the API.

Once you have a registered application then you can generate the "Keys and tokens" required to get started.

Nuget Package

Once you have registered for your developer account and generated your keys and tokens we next need to reference the nuget package in your application - https://www.nuget.org/packages/SocialOpinionAPI/

This can be done through the .NET CLI

dotnet add package SocialOpinionAPI --version 2.0.12

Or through nuget package manager console in Visual Studio.

Install-Package SocialOpinionAPI -Version 2.0.12

Or through the nuget package management UI in Visual Studio if that is more your thing.

Getting User Info

The following assumes we're in a simple .NET console application written with Top Level Statements.

What are we going to do? With this introduction to get started we are going to make a request to get the user information for your account. This will show an example of a requesting the profile information of me @westdiscgolf .

First off we will need to add some using statements. As we are going to use the OAuthInfo POCO class we will require the "Core" namespace.

using SocialOpinionAPI.Core;

In addition to this as we are wanting to execute the ability to get the specific user information we will be wanting to use the UserService and that is found in the "Users" namespace.

using SocialOpinionAPI.Services.Users;

Now we need to create an OAuthInfo instance so we can use the information to make the requests to the API.

OAuthInfo oAuthInfo = new OAuthInfo
{
    AccessSecret = "** secret **",
    AccessToken = "** token **",
    ConsumerSecret = "** secret **",
    ConsumerKey = "** key **"
};
Enter fullscreen mode Exit fullscreen mode

For simple testing we can hard code the generated tokens and secrets into the object instance above. However for best practices these values should be kept in configuration in a secure location which is encrypted in production. For example if in an ASP.NET Core web application context these maybe stored in your user secrets locally but then loaded out of encrypted app settings or Azure Key Vault when deployed to production. This will come down to how you deploy your application. Don't check them into source control!!

Now we have our auth info we can create an instance of a UserService.

var userService = new UserService(oAuthInfo);
Enter fullscreen mode Exit fullscreen mode

The user service gives you access to work with the user based information. The service has methods for GetUser, GetUsers, GetFollowers and GetFollowing. In this example we're interested in the GetUser method to request the user profile for the specified handle. Under the hood this uses the get by username end point - https://api.twitter.com/2/users/by/username/

To make a request for a user profile is as straight forward as calling the GetUser method with my Twitter handle.

var me = userService.GetUser("WestDiscGolf");
Enter fullscreen mode Exit fullscreen mode

And that is it. This returns the information about the handle requested in a strongly typed object model ready to then use it programatically.

Conclusion

I'm really excited to see this API package make accessing the Twitter API relatively straight forward. I'm looking forward to playing some more and seeing what else it can do!

Are you looking at using Twitter data for something? Are you already using it for your next project? Let me know on Twitter @WestDiscGolf!

If you're interested in other .NET Core, Azure and Testing content then checkout http://adamstorr.co.uk

Top comments (0)