如何使用 Microsoft graph API 来验证和列出用户/运行不同的 graph ql api

问题描述 投票:0回答:1

我已经在azure云控制台中创建了企业应用程序/应用程序注册。并创建了 Web 凭证客户端密钥。

https://learn.microsoft.com/en-us/azure/active-directory/manage-apps/add-application-portal

我正在使用 microsoft go sdks 使用客户端密钥进行身份验证,并获取用户列表/应用程序列表/图形客户端当前用户,它们是我之前创建的企业应用程序的一部分。请找到下面的代码。但是,每次调用 go sdk 时,以下代码都会返回错误。

package main

import (
    "context" 
    "fmt" 
    "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
    "github.com/Azure/go-autorest/autorest/to" 
    msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
    msgraphcore "github.com/microsoftgraph/msgraph-sdk-go-core" 
    a "github.com/microsoftgraph/msgraph-sdk-go-core/authentication" 
    "github.com/microsoftgraph/msgraph-sdk-go/applications" 
    "github.com/microsoftgraph/msgraph-sdk-go/models" 
    "github.com/microsoftgraph/msgraph-sdk-go/users" 
    "log" 
)

func main() {

    cred, err := azidentity.NewClientSecretCredential("dd....", "10...", "LV....", nil)
    
    if err != nil {
        log.Fatal(err)
    }
    
    auth, err := a.NewAzureIdentityAuthenticationProviderWithScopes(cred, []string{"https://graph.microsoft.com/.default"})
    if err != nil {
        log.Fatal(err)
    }
    requestAdapter, err := msgraphsdk.NewGraphRequestAdapter(auth)
    if err != nil {
        log.Fatal(err)
    }
    
    graphClient := msgraphsdk.NewGraphServiceClient(requestAdapter)
    query := users.UserItemRequestBuilderGetQueryParameters{
        Select: []string{"displayName", "jobTitle"},
    }
    
    options := users.UserItemRequestBuilderGetRequestConfiguration{
        QueryParameters: &query,
    }
    
    result, err := graphClient.Me().Get(context.Background(), &options)
    if err != nil {
        fmt.Printf("Error getting users: %v\n", err)
        log.Fatal(err)
    }
    
    appGetOptions := &applications.ApplicationsRequestBuilderGetRequestConfiguration{
        QueryParameters: &applications.ApplicationsRequestBuilderGetQueryParameters{
            Filter: to.StringPtr(getDisplayNameFilter("swagger")),
        },
    }
    
    _, err_ := graphClient.Applications().Get(context.Background(), appGetOptions)
    if err_ != nil {
        log.Fatal(err_)
    }

}

func getDisplayNameFilter(displayName string) string {
 return fmt.Sprintf("displayName eq '%s'", displayName) 
}

当我运行上述代码时,遇到以下错误,响应代码为 0。

获取用户时出错:从 API 收到错误状态代码

您能否让我知道我做错了什么导致运行程序时出现错误?

go microsoft-graph-api azure-ad-graph-api microsoft-graph-sdks azure-application-registration
1个回答
0
投票

使用客户端密钥时,您无法获取当前用户的详细信息,因为您没有以任何用户身份登录。打电话

graphClient.Me()
永远不会起作用。您只能获取所有用户的信息。

import (
      "context"
      msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
      graphusers "github.com/microsoftgraph/msgraph-sdk-go/users"
      //other-imports
)

cred, err := azidentity.NewClientSecretCredential("dd....", "10...", "LV....", nil)

if err != nil {
    log.Fatal(err)
}

auth, err := a.NewAzureIdentityAuthenticationProviderWithScopes(cred, []string{"https://graph.microsoft.com/.default"})
if err != nil {
    log.Fatal(err)
}
requestAdapter, err := msgraphsdk.NewGraphRequestAdapter(auth)
if err != nil {
    log.Fatal(err)
}

graphClient := msgraphsdk.NewGraphServiceClient(requestAdapter)

requestParameters := &graphusers.UsersRequestBuilderGetQueryParameters{
    Select: [] string {"displayName","jobTitle"},
}
configuration := &graphusers.UsersRequestBuilderGetRequestConfiguration{
    QueryParameters: requestParameters,
}

users, err := graphClient.Users().Get(context.Background(), configuration)
© www.soinside.com 2019 - 2024. All rights reserved.