OAuth - adamyeager/PushbulletSharp GitHub Wiki

This document outlines how to use PushBulletSharp with PushBullet's OAuth API. You can read more about OAuth on PushBullet's API site.

The idea is to first setup an application with their create client page. Doing this you will gain access to a client_id and client_secret. You will need this information for the OAuthTokenRequest object.

Once you've setup a client, you should see on the client page a link to test your OAuth for the client you just created (it should say something like oauth test url). That URL is almost perfect for PushBulletSharp. The only thing you'll need to change is the response type. The URL by default is set to response_type=token. Change it to response_type=code. This will redirect to your redirect page with a code that you'll need for the OAuthTokenRequest object.

The OAuthTokenRequest object has four properties: grant_type, client_id, client_secret, & code. By default, according to PushBullet's API documentation, grant_type should be set to "authorization_code". For this reason, the contructor of the OAuthTokenRequest object sets the grant_type to "authorization_code". So when you create an instance of OAuthTokenRequest, all you need to set is the client_id, client_secret, & code. The client_id and client_secret are things you should already have from the create client page. The code is what you will get from the URL on the redirect page when a user authorizes your application.

Once you have that code, create an instance of the OAuthTokenRequest object with the code, client_id, and client_secret and use the method RequestToken on the PushBulletClient. If everything goes well, you will get a OAuthTokenResponse which will include token_type and access_token. The token_type will be Bearer and the access_token is what you'll want to store for your authenticated user as the token you can use to push notifications to them from your application.

To push notifications to a user using their access token, construct a PushBulletClient object with their access token as the access token. If you already have a PushBulletClient object created, you can change the access token by accessing the PushBulletClient.AccessToken property.

An example of doing this can be found in the Tests project in the OAuthTests unit test class.

Models.Requests.OAuthTokenRequest request = new Models.Requests.OAuthTokenRequest()
{
    client_id = "--YOUR APP CLIENT ID--",
    client_secret = "--YOUR APP CLIENT SECRET--",
    code = "--USER CODE FROM REDIRECT--"
};

var result = Client.RequestToken(request);

Client.AccessToken = result.access_token;
var oauthUserInformation = Client.CurrentUsersInformation();
var oauthTestPushResults = Client.PushNote(new Models.Requests.PushNoteRequest()
{
    title = "OAuth Push Test",
    body = "This is a test push using OAuth!",
    email = oauthUserInformation.email
});