Development, Salesforce

Introducing forceGOAuth

Go, or golang, is a language I thought looked strange and sloppy, but with my long C-development background, it didn’t take me many hours to start loving the language and the large community growing around it. As a Salesforce developer and user, I was, of course, curious to find an easy way to start working with the different platform APIs.

A great starting point for this journey was to look closer at the force CLI tool developed by the guys at Heroku and Salesforce. The next step was to start building my own application. Pretty soon, I realized that I had implemented the full webserver OAuth flow in my application—and all for a token so that I could call the APIs.

This was not what I wanted to do every time I needed to throw together a small Go app to fetch something from the Salesforce platform. I decided to refactor the application code and move all the OAuth code out to a library so that I easily could reuse it. forceGOAuth was born. And since this is still very much of a learning experience for me, I decided to publish the code on GitHub with an open source license so that other people can benefit from it and help me to improve it by raising issues or submitting pull requests.

Do you like the content on this page? Sign up for our newsletter
to make sure you are not missing out!

The API fully supports the webserver and username/password flows, but it can also facilitate the user agent flow by generating the token URLs. A token can be generated with just a few lines of code, but the library was also extended to support a few basic HTTP calls to speed up the development process even more.

The following example shows the full webserver flow:

OAuthSecurity := forceGOAuth.NewOAuthSecurity()
OAuthSecurity.OAuthBaseURL = "https://test.salesforce.com/services/oauth2"
OAuthSecurity.ConsumerKey = "XXXX"
OAuthSecurity.ConsumerSecret = "YYYY"

err := OAuthSecurity.DoFullWebServerFlow()
if err != nil {
    fmt.Printf("DoFullWebflow failed: %s\n", err)
    return
}

This snippet will do the username and password flow:

OAuthSecurity := forceGOAuth.NewOAuthSecurity()
OAuthSecurity.OAuthBaseURL = "https://test.salesforce.com/services/oauth2"
OAuthSecurity.ConsumerKey = "XXXX"
OAuthSecurity.ConsumerSecret = "YYYY"
OAuthSecurity.UserName = "user@domain.com"
OAuthSecurity.Password = "supersecret"

err := OAuthSecurity.DoFullUNFlow()
if err != nil {
    fmt.Printf("DoFullUNFlow failed: %s\n", err)
    return
}

When a token is received, the Salesforce REST APIs can be called by one of the Get, Post, Patch, or Delete methods that automatically use the token and also try to refresh it if has expired. The API aims to take the setup away so that the developer can spend the time to build business logic instead of rebuilding the OAuth authentication. You are not limited to use the built-in method and can use the library only to obtain the token if necessary.

There is test code written for the library, and it will be extended as new features and issues are added.

You can find the library at this GitHub repository.

 

This blog post is a repost from Martin’s old blog called webaholic.

Leave a Reply

Your email address will not be published. Required fields are marked *