1

So I'm testing the waters with Go. I need to manually make a REST call to an Azure AD protected endpoint. I'm using the Azure Identity package, but still I am not able to get the token.

package main

import (
    "context"
    "fmt"

    azi "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
)

func main() {

    cred, err := azi.NewInteractiveBrowserCredential(nil)
    if err != nil {
        fmt.Println(err.Error())
        return
    }

    fmt.Println("No error ")
    var ctx = context.Context()
    fmt.Println(cred.GetToken(ctx))
}

This then yields the following error response

# command-line-arguments
.\main.go:19:27: missing argument to conversion to context.Context: context.Context()

Can someone please point me in the right direction of what I am doing wrong?

baouss
  • 1,312
  • 1
  • 22
  • 52
  • Thanks sbrichards, using `var ctx = context.Background()` helped to provide one parameter to the `GetToken()` method. The second required argument is of type `shared.TokenRequestOptions`. If I import this from `github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/shared` I'm yelled at, that ` main.go:7:2: use of internal package github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/shared not allowed` – baouss Feb 23 '22 at 12:48
  • 1
    I solved it with `policy := policy.TokenRequestOptions{Scopes: []string{"MYSCOPE"}}` – baouss Feb 23 '22 at 13:26

1 Answers1

1

context.Context is an interface, not a method (https://pkg.go.dev/context#Context) that is why you're getting the error, you're attempting to convert nothing to that type.

Calls to the GetToken method require something that implements context.Context.

Try replacing var ctx = context.Context() with var ctx = context.Background()

Read more about context.Context here https://pkg.go.dev/context

sbrichards
  • 2,169
  • 2
  • 19
  • 32