wide-machine-60917
08/15/2023, 10:30 AMchilly-king-10285
08/15/2023, 10:30 AMchilly-king-10285
08/15/2023, 10:31 AMgo get <http://github.com/ory/client-go|github.com/ory/client-go>
For Ory Kratos: go get <http://github.com/ory/kratos-client-go@<version-you-want|github.com/ory/kratos-client-go@<version-you-want>>
Replace <version-you-want>
with the version number you want to install. 2. Set up and configure Ory using the Go SDK: Here's an example of how to do this for Ory Hydra: go package main import ( client "<http://github.com/ory/hydra-client-go|github.com/ory/hydra-client-go>" ) func main() { configuration := client.NewConfiguration() configuration.Servers = []client.ServerConfiguration{ { URL: "<http://localhost:4445>", // Admin API URL }, } // admin := client.NewAPIClient(configuration) // admin.OAuth2Api.CreateOAuth2Client(... }
Replace "<http://localhost:4445>"
with the URL of your Admin API. 3. Make authenticated requests: Here's an example of how to make an authenticated request to create an OAuth2 client: go package main import ( "context" "fmt" "net/http" "os" ory "<http://github.com/ory/client-go|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() // handle error and response... }
Replace "<https://practical-swirles-whg26u2ofh.projects.oryapis.com>"
with the URL of your Admin API, and replace "ORY_API_KEY"
with your actual API key. For more information, refer to the [Ory Hydra Go SDK documentation](https://www.ory.sh/docs/hydra/sdk/go) and the [Ory Kratos Go SDK documentation](https://www.ory.sh/docs/kratos/self-hosted/go).wide-machine-60917
08/15/2023, 10:38 AMchilly-king-10285
08/15/2023, 10:38 AMchilly-king-10285
08/15/2023, 10:39 AMjavascript const fetch = require("node-fetch") const accessToken = "your_access_token_here" fetch("<https://api.example.com/resource>", { headers: { Authorization: "Bearer " + accessToken, }, }) .then((response) => response.json()) .then((data) => console.log(data)) .catch((error) => console.error(error))
In this example, the access token is included in the Authorization
header of the GET request sent to the resource server API endpoint. The Bearer
keyword indicates that the token is a bearer token, which is a type of access token that can be used by any client to access the protected resources. The resource server then verifies the token and grant or deny access based on the token's validity and the scope of access that was granted. The resource server needs to ensure that the access token is valid and has not been tampered with or expired before allowing access to protected resources. To achieve this, the resource server uses the OAuth2 token introspection endpoint, which is an API endpoint exposed by Ory. For more information, refer to the [Ory documentation](https://www.ory.sh/docs/oauth2-oidc/overview/oauth2-concepts).wide-machine-60917
08/15/2023, 10:39 AMchilly-king-10285
08/15/2023, 10:39 AMchilly-king-10285
08/15/2023, 10:40 AMgo package main import ( "fmt" "net/http" "io/ioutil" ) func main() { url := "<http://example.com>" method := "GET" client := &http.Client { } req, err := http.NewRequest(method, url, nil) if err != nil { fmt.Println(err) return } req.Header.Add("Authorization", "Bearer YOUR_TOKEN") res, err := <http://client.Do|client.Do>(req) if err != nil { fmt.Println(err) return } defer res.Body.Close() body, err := ioutil.ReadAll(res.Body) if err != nil { fmt.Println(err) return } fmt.Println(string(body)) }
In this example, replace "<http://example.com>"
with the URL of your API, "GET"
with the HTTP method you want to use, and "Bearer YOUR_TOKEN"
with "Bearer "
followed by your actual token. This code creates a new HTTP request, adds the Authorization header with the bearer token, sends the request, and then prints the response body. Please note that this is a simple example and doesn't include error handling or other best practices for production code.wide-machine-60917
08/15/2023, 10:41 AMchilly-king-10285
08/15/2023, 10:41 AMchilly-king-10285
08/15/2023, 10:42 AMDefaultHeader
field in the Configuration
struct to set the Authorization
header. Here's an example: go package main import ( "context" "fmt" "io" ory "<http://github.com/ory/client-go|github.com/ory/client-go>" ) func main() { client := ory.NewAPIClient(&ory.Configuration{ Servers: ory.ServerConfigurations{{ URL: "https://{your-project-slug}.<http://projects.oryapis.com|projects.oryapis.com>", }}, DefaultHeader: map[string]string{ "Authorization": "Bearer YOUR_TOKEN", // Replace with your token }, }) // Replace with the actual API call req, res, err := client.SomeApi.SomeMethod(context.Background()).Execute() if err != nil { body, _ := io.ReadAll(res.Body) fmt.Printf("Error: %v: %v", err.Error(), string(body)) panic(err) } fmt.Printf("Response: %v\n", req) }
In this example, replace {your-project-slug}.<http://projects.oryapis.com|projects.oryapis.com>
with the URL of your Ory project, Bearer YOUR_TOKEN
with Bearer
followed by your actual token, and client.SomeApi.SomeMethod(context.Background()).Execute()
with the actual API call you want to make. This code creates a new Ory API client with the specified configuration, makes the API call, and then prints the response. Please note that this is a simple example and doesn't include error handling or other best practices for production code. For more information, refer to the [Ory Go SDK documentation](https://github.com/ory/client-go).