0% found this document useful (0 votes)
2 views

3

This document provides instructions for generating API client classes using the Kiota command line tool and registering an application with the Microsoft identity platform for Microsoft Graph authentication. It outlines the steps to create an app registration in the Azure Active Directory, including setting up device code flow and creating a client application in Go. Finally, it includes code examples and commands to run the application to retrieve user information from Microsoft Graph.

Uploaded by

Yusuf D M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

3

This document provides instructions for generating API client classes using the Kiota command line tool and registering an application with the Microsoft identity platform for Microsoft Graph authentication. It outlines the steps to create an app registration in the Azure Active Directory, including setting up device code flow and creating a client application in Go. Finally, it includes code examples and commands to run the application to retrieve user information from Microsoft Graph.

Uploaded by

Yusuf D M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

properties:

id:
type: string
displayName:
type: string
You can then use the Kiota command line tool to generate the API client classes.

Bash

Copy
kiota generate -l go -d ../get-me.yml -c GraphApiClient -n getuser/client -o
./client
Register an application
To be able to authenticate with the Microsoft identity platform and get an access
token for Microsoft Graph, you need to create an application registration. You can
install the Microsoft Graph PowerShell SDK and use it to create the app
registration, or register the app manually in the Azure Active Directory admin
center.

The following instructions register an app and enable device code flow for
authentication.

Azure portal
PowerShell
Open a browser and navigate to the Azure Active Directory admin center. Sign in
with your Azure account.

Select Azure Active Directory in the left-hand navigation, then select App
registrations under Manage.

Select New registration. On the Register an application page, set the values as
follows.

Set Name to Kiota Test Client.


Set Supported account types to Accounts in any organizational directory and
personal Microsoft accounts.
Leave Redirect URI blank.
Select Register. On the Overview page, copy the value of the Application (client)
ID and save it.

Select Authentication under Manage.

Locate the Advanced settings section. Set the Allow public client flows toggle to
Yes, then select Save.

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.

Go

Copy
package main

import (
"context"
"fmt"

"getuser/client"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
azure "github.com/microsoft/kiota-authentication-azure-go"
bundle "github.com/microsoft/kiota-bundle-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 := bundle.NewDefaultRequestAdapter(authProvider)

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

client := client.NewGraphApiClient(adapter)

me, err := client.Me().Get(context.Background(), 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

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

Copy
go run .
See also
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.

You might also like