通过 Graph 向 Teams 中的频道发送消息

Send a message to a Channel in Teams via Graph

我尝试将消息发送到团队中的特定频道。 它与 'https://developer.microsoft.com/de-de/graph/graph-explorer'.

完美配合

为了获得访问令牌,我执行以下操作。这部分有效,因为我可以用它检索用户信息和其他东西。

$guzzle = new \GuzzleHttp\Client(['verify' => false]);
$tenantId = '<tenant_id>';

$clientId = '<client_id>';
$clientSecret = '<client_secret>';
$url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/token?api-version=1.0';
$token = json_decode($guzzle->post($url, [
        'form_params' => [
            'client_id' => $clientId,
            'client_secret' => $clientSecret,
            'resource' => 'https://graph.microsoft.com/',
            'grant_type' => 'client_credentials',
    ],
    'verify' => false
])->getBody()->getContents());
$accessToken = $token->access_token;

这部分现在是产生错误的部分

$graph = new Graph();
$graph->setAccessToken($accessToken);

$url = '/teams/<team_id>/channels/<channel_id>/messages';


$queryParams = [
    'body' => [
        'content' => 'foo'
    ]
];    
$info = $graph->setApiVersion("beta")->createRequest("POST", $url)
    ->addHeaders(array("Content-Type" => "application/json"))
    ->attachBody($queryParams)
    ->setReturnType(BetaModel\User::class)
    ->execute();
echo '<pre>';
print_r($info);

这会产生以下错误

{
  "error": {
    "code": "UnknownError",
    "message": "",
    "innerError": {
      "date": "2020-11-11T10:14:03",
      "request-id": "<request_id>",
      "client-request-id": "<client_request_id>"
    }
  }
}

团队ID和频道ID是正确的,我可以使用

检索它们
/users/<user_id>/joinedTeams

/teams/<teams_id>/channels

如前所述,它在图形资源管理器页面中有效。 我知道我需要以某种方式在用户上下文中发送消息,但我不能让它工作,而且我找不到有用的信息,所以我很高兴。提前致谢!

此 api 调用目前不支持 应用程序权限 ,因此您无法使用客户端凭据流获取令牌。您需要向应用程序授予委派权限,然后使用auth code flow to obtain the token. see:here.