Link Search Menu Expand Document (external link)

Build SDKs for Go

Required tools

Target project requirements

Before you can compile and run the target project, you will need to initialize it. After initializing the test project, you will need to add references to the abstraction, authentication, http, serialization JSON, and serialization Text packages.

Creating target projects

Note: you can use an existing project if you have one, in that case, you can skip the following section.

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

go mod init getuser

Adding dependencies

Create a file named getuser.go and add the following code.

go get github.com/microsoft/kiota-abstractions-go
go get github.com/microsoft/kiota-http-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-authentication-azure-go
go get github.com/Azure/azure-sdk-for-go/sdk/azidentity

Only the first package, github.com/microsoft/kiota-abstractions-go, is required. The other packages provide default implementations that you can choose to replace with your own implementations if you wish.

Generating the SDK

Kiota generates SDKs from OpenAPI documents. Create a file named getme.yml and add the contents of the Sample OpenAPI description.

You can then use the Kiota command line tool to generate the SDK classes.

kiota -l go -d ../getme.yml -c GraphApiClient -n getuser/client -o ./client

Creating an application registration

Note: this step is required if your client will be calling APIs that are protected by the Microsoft Identity Platform like Microsoft Graph.

Follow the instructions in Register an application for Microsoft identity platform authentication to get an application ID (also know as a client ID).

Creating 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.

package main

import (
  "context"
  "fmt"

  "getuser/client"

  "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
  azure "github.com/microsoft/kiota-authentication-azure-go"
  http "github.com/microsoft/kiota-http-go"
)

func main() {
  clientId := "YOUR_CLIENT_ID"

  // The auth provider will only authorize requests to
  // the allowed hosts, in this case Microsoft Graph
  allowedHosts := []string{"graph.microsoft.com"}
  graphScopes := []string{"User.Read"}

  credential, err := azidentity.NewDeviceCodeCredential(&azidentity.DeviceCodeCredentialOptions{
    ClientID: clientId,
    UserPrompt: func(ctx context.Context, dcm azidentity.DeviceCodeMessage) error {
      fmt.Println(dcm.Message)
      return nil
    },
  })

  if err != nil {
    fmt.Printf("Error creating credential: %v\n", err)
  }

  authProvider, err := azure.NewAzureIdentityAuthenticationProviderWithScopesAndValidHosts(
    credential, graphScopes, allowedHosts)

  if err != nil {
    fmt.Printf("Error creating auth provider: %v\n", err)
  }

  adapter, err := http.NewNetHttpRequestAdapter(authProvider)

  if err != nil {
    fmt.Printf("Error creating request adapter: %v\n", err)
  }

  client := client.NewGraphApiClient(adapter)

  me, err := client.Me().Get(nil)

  if err != nil {
    fmt.Printf("Error getting user: %v\n", err)
  }

  fmt.Printf("Hello %s, your ID is %s\n", *me.GetDisplayName(), *me.GetId())
}

Note:

  • If the target API doesn’t require any authentication, you can use the AnonymousAuthenticationProvider instead.
  • If the target API requires an Authorization: Bearer <token> header but doesn’t rely on the Microsoft Identity Platform, you can implement your own authentication provider by inheriting from BaseBearerTokenAuthenticationProvider.
  • If the target API requires any other form of authentication schemes, you can implement the AuthenticationProvider interface.

Executing the application

When ready to execute the application, execute the following command in your project directory.

go run .

See also