代码之家  ›  专栏  ›  技术社区  ›  Amit Kanala

我尝试在microsoft outlook日历中创建事件,但出现错误,访问令牌验证失败。无效受众

  •  1
  • Amit Kanala  · 技术社区  · 8 月前

    我已尝试在microsoft outlook日历中创建事件,但出现错误

     {"error":{"code":"InvalidAuthenticationToken","message":"Access token validation failure. Invalid audience.","innerError":{"date":"2025-01-02T12:23:03","request-id":"8e700f58-302f-4aad-bb63-eb5283171d1d","client-request-id":"8e700f58-302f-4aad-bb63-eb5283171d1d"}}}
    

    这是我的代码,我们完美地生成了访问令牌,并在jwt.io网站上进行了测试,完美令牌得到了验证,但当我在上面的错误代码中添加了这个令牌,并且我在我的azure门户目录中添加了所有类型的权限时,我想我错过了一件正常的事情,但我不知道是什么

    $accessToken='这是jwt.io网站测试的经过测试的有效签名完美令牌' $targetUserId=“我的outlook电子邮件地址”

    // Event details
            $eventDetails = [
                'summary' => 'Event Subject',
                'description' => 'Event Description',
                'start' => [
                    'dateTime' => date('Y-m-d\TH:i:s', time()), // current date and time
                    'timeZone' => 'UTC', // adjust time zone if needed
                ],
                'end' => [
                    'dateTime' => date('Y-m-d\TH:i:s', time() + 3600), // one hour later
                    'timeZone' => 'UTC', // adjust time zone if needed
                ],
                'location' => 'Event Location',
            ];
    
            // Prepare the data for the API request
            $postFields = json_encode([
                'subject' => $eventDetails['summary'],
                'body' => [
                    'contentType' => 'HTML',
                    'content' => $eventDetails['description'],
                ],
                'start' => [
                    'dateTime' => $eventDetails['start']['dateTime'],
                    'timeZone' => $eventDetails['start']['timeZone'],
                ],
                'end' => [
                    'dateTime' => $eventDetails['end']['dateTime'],
                    'timeZone' => $eventDetails['end']['timeZone'],
                ],
                'location' => [
                    'displayName' => $eventDetails['location'],
                ],
            ]);
    
            // Set the cURL options for the POST request to the Graph API
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "https://graph.microsoft.com/v2.0/users/{$targetUserId}/events");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $headers = array();
            $headers[] = 'Authorization: Bearer '.$accessToken;
            $headers[] = 'Content-Type: application/json';
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            //~ curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization' => 'Bearer '.$accessToken, 'Content-Type' => 'application/json']);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
            curl_setopt($ch, CURLOPT_VERBOSE, true);
    
            // Execute the request and get the response
            $response = curl_exec($ch);
            curl_close($ch);
    
            // Handle the response (error handling and logging may be added here)
            if ($response === false) {
                echo 'Error creating event.';
            } else {
                echo 'Event created successfully: ' . $response;
            }
    

    令牌生成代码

    curl --location --request POST 'https://login.microsoftonline.com/$tenant-id/oauth2/v2.0/token' \
    --header 'Content-Type: application/x-www-form-urlencoded' \
    --data-urlencode 'client_id=f66eccc4-7397....' \
    --data-urlencode 'scope=f66eccc4-7397-4c3a-9999-36d2d3156788/.default' \
    --data-urlencode 'refresh_token=1.ASwA67m7Og_3jUOZPNoq_CfpiMTMbvaXczpMmZk20tMVZ4gsAMIsAA.AgABAwEAAADW6jl31mB3......' \
    --data-urlencode 'grant_type=refresh_token' \
    --data-urlencode 'client_secret=45Y8Q~Xw4Pj3GN65J.......'
    
    1 回复  |  直到 8 月前
        1
  •  1
  •   juunas    8 月前

    这行不通:

    scope=f66eccc4-7397-4c3a-9999-36d2d3156788/.default
    

    您对Entra ID说,您想要一个访问令牌目标 您的API ,而不是Graph API。

    您需要使用它来获取Graph API令牌:

    scope=https://graph.microsoft.com/.default
    

    这假设您已经在应用程序注册的API权限选项卡中设置了所需的权限。

    推荐文章