Making use of OAuth2 clients
OAuth2 is a relatively common protocol for speaking with APIs. The golang.org/x/oauth2
package provides a pretty flexible client for working with OAuth2. It has subpackages that specify endpoints for various providers such as Facebook, Google, and GitHub.
This recipe will demonstrate how to create a new GitHub OAuth2 client and some of its basic usage.
Getting ready
Configure your environment according to these steps:
- Refer to the Getting ready section of the Initializing, storing, and passing http.Client structs recipe.
- Run the
go get golang.org/x/oauth2
command. - Configure an OAuth Client at https://github.com/settings/applications/new.
- Set the environment variables with your Client ID and Secret:
export GITHUB_CLIENT="your_client"
export GITHUB_SECRET="your_secret"
- Brush up on the GitHub API documentation at https://developer.github.com/v3/.
How to do it...
These steps cover writing and running your application:
- From your terminal/console application, create and navigate...