Skip to main content

Ory OAuth2 & OpenID Connect Go SDK

In this document you can find code examples for the Ory OAuth2 Go SDK.

info

Missing an example? Please create a feature request and it will be added here.

You can find more examples of SDK usage in the auto-generated documentation hydra-client.

The Ory OAuth2 Go SDK is generated using go-swagger.

danger

Don't consume the /oauth2/auth and /oauth2/token endpoints using this SDK. Use golang.org/x/oauth2. For more information visit the Using OAuth2 guide.

Installation

To install the Go SDK, run:

go get github.com/ory/client-go@master

Making requests

The following code examples show how to make requests to the Ory OAuth2 API.

CreateOAuth2Client and ListOAuth2Clients

In this example the admin API is called to create a new OAuth2 client request is used to create an OAuth 2.0 client, as well as looping through existing clients and listing them.

package main

import (
"context"
"fmt"
"net/http"
"os"

ory "github.com/ory/client-go"
)

// Use this context to access Ory APIs which require an Ory API Key.
var oryAuthedContext = context.WithValue(context.Background(), ory.ContextAccessToken, os.Getenv("ORY_API_KEY"))

func main() {
clientName := "example_client"
oAuth2Client := *ory.NewOAuth2Client() // OAuth2Client |
oAuth2Client.SetClientName(clientName)

configuration := ory.NewConfiguration()
configuration.Servers = []ory.ServerConfiguration{
{
URL: "https://practical-swirles-whg26u2ofh.projects.oryapis.com", // Public API URL
},
}
ory := ory.NewAPIClient(configuration)
resp, r, err := ory.OAuth2Api.CreateOAuth2Client(oryAuthedContext).OAuth2Client(oAuth2Client).Execute()
if err != nil {
switch r.StatusCode {
case http.StatusConflict:
fmt.Fprintf(os.Stderr, "Conflict when creating oAuth2Client: %v\n", err)
default:
fmt.Fprintf(os.Stderr, "Error when calling `OAuth2Api.CreateOAuth2Client`: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
}
// response from `CreateOAuth2Client`: OAuth2Client
fmt.Fprintf(os.Stdout, "Created client with name %s\n", resp.GetClientName())

clients, r, err := ory.OAuth2Api.ListOAuth2Clients(oryAuthedContext).ClientName(clientName).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `AdminApi.ListOAuth2Clients``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
fmt.Fprintf(os.Stdout, "We have %d clients\n", len(clients))
for i, client := range clients {
fmt.Fprintf(os.Stdout, "Client %d name: %s\n", i+1, client.GetClientName())
// Add any additional information you want to display about the client
}
}

More examples

You can find more examples of SDK usage in the autogenerated documentation: