title | description | author | ms.author | ms.topic | date |
---|---|---|---|---|---|
Build API clients for Go with Microsoft identity authentication |
Learn how use Kiota to build API clients in Go to access APIs that require Microsoft identity authentication. |
jasonjoh |
jasonjoh |
tutorial |
03/20/2023 |
In this tutorial, you generate an API client that uses Microsoft identity authentication to access Microsoft Graph.
Run the following commands in the directory where you want to create a new project.
go mod init getuser
Before you can compile and run the generated API client, you need to make sure the generated source files are part of a project with the required dependencies. Your project must have a reference to the abstraction package. Additionally, you must either use the Kiota default implementations or provide your own custom implementations of the following packages.
- Authentication (Kiota default Azure authentication)
- HTTP (Kiota default net/http-based implementation)
- Form serialization (Kiota default)
- JSON serialization (Kiota default)
- Text serialization (Kiota default)
- Multipart serialization (Kiota default)
For this tutorial, use the default implementations.
Run the following commands to get the required dependencies.
go get github.com/microsoft/kiota-abstractions-go
go get github.com/microsoft/kiota-http-go
go get github.com/microsoft/kiota-serialization-form-go
go get github.com/microsoft/kiota-serialization-json-go
go get github.com/microsoft/kiota-serialization-text-go
go get github.com/microsoft/kiota-serialization-multipart-go
go get github.com/microsoft/kiota-authentication-azure-go
go get github.com/Azure/azure-sdk-for-go/sdk/azidentity
Kiota generates API clients from OpenAPI documents. Create a file named get-me.yml and add the following.
:::code language="yaml" source="~/code-snippets/get-started/azure-auth/get-me.yml":::
You can then use the Kiota command line tool to generate the API client classes.
kiota generate -l go -d ../get-me.yml -c GraphApiClient -n getuser/client -o ./client
[!INCLUDE register-azure-app-device-code-intro]
[!INCLUDE register-azure-app-device-code-portal]
[!INCLUDE register-azure-app-device-code-powershell]
Create a file in the root of the project named getuser.go and add the following code. Replace YOUR_CLIENT_ID
with the client ID from your app registration.
:::code language="go" source="~/code-snippets/get-started/azure-auth/go/getuser.go" id="ProgramSnippet":::
Note
This example uses the DeviceCodeCredential class. You can use any of the credential classes from the azidentity
library.
To start the application, run the following command in your project directory.
go run .
- kiota-samples repository contains the code from this guide.
- ToDoItem Sample API implements a sample OpenAPI in ASP.NET Core and sample clients in multiple languages.