Skip to content

Latest commit

 

History

History
100 lines (66 loc) · 4.04 KB

go-azure.md

File metadata and controls

100 lines (66 loc) · 4.04 KB
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

Build API clients for Go with Azure authentication

In this tutorial, you generate an API client that uses Microsoft identity authentication to access Microsoft Graph.

Required tools

Create a project

Run the following commands in the directory where you want to create a new project.

go mod init getuser

Add dependencies

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.

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

Generate the API client

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

Register an application

[!INCLUDE register-azure-app-device-code-intro]

[!INCLUDE register-azure-app-device-code-portal]

[!INCLUDE register-azure-app-device-code-powershell]


Create the client application

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.

Run the application

To start the application, run the following command in your project directory.

go run .

See also