我已经设置了 contact.read.write 的 MS Azure API 权限,但是在执行以下代码时,我收到此消息:
Uncaught GuzzleHttp\Exception\ClientException: Client error:
POST https://graph.microsoft.com/beta/contacts
resulted in a400 Bad Request
response: { "error": { "code": "Request_BadRequest", "message": "A value without a type name was found and no expecte (truncated...) in C:\Program Files\Ampps\www\weedo\weedo2\content\libraries\MicrosoftGraph\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:113
我的邮政编码:
$graph = new Graph();
$graph
->setBaseUrl("https://graph.microsoft.com/")
->setApiVersion("beta")
->setAccessToken($this->accessToken);
$data = array(
"GivenName" => "Test",
"Surname" => "account",
"EmailAddresses" => array(
array(
"Address" => "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b2f3e282f3a3838342e352f1b363a323775383436" rel="noreferrer noopener nofollow">[email protected]</a>",
"Name" => "Mail Name"
)
)
);
$user = $graph->createRequest("POST", "/contacts")
->addHeaders(array("Content-Type" => "application/json"))
->setReturnType(Model\User::class)
->attachBody($data)
->setTimeout("1000")
->execute();
我相信我正确地遵循了这些页面,但示例并未全部针对 PHP,因此我不确定从这里开始: https://github.com/microsoftgraph/msgraph-sdk-php https://learn.microsoft.com/en-us/graph/api/user-post-contacts?view=graph-rest-1.0&tabs=http
我还将 $data 参数包装在 json_encode 中只是为了测试,但没有运气。 去哪儿?该怎么办?
编辑: 因此,我似乎对组织联系人(通过/contacts 获得)和个人联系人(通过/me/contacts 获得)感到困惑。现在(尚)不支持创建组织联系人,我觉得这很荒谬,但由于某种原因,我也不允许基于应用程序创建联系人。对此有什么想法/想法吗?
最佳答案
你说得对,目前没有Microsoft Graph API对于创建组织联系人,仅提供列出和获取方法。当我尝试创建一个新的时,我收到此错误:
此问题也已得到 a Microsoft engineer here 的确认.
因此,我知道添加组织联系人的唯一两种方法是 Microsoft 365 admin Center =>Users => Contacts blade手动添加:
将我自己添加为组织联系人,我可以通过 /contacts
调用获取新记录:
第二种方法是使用the PowerShell command:New-MailContact去做这个:
所有这两种方法我都已经在我身边进行了测试,并且都适合我。
更新:
如果您正在寻找有关通过 Azure AD 应用程序添加用户个人联系人的示例代码,只需尝试以下代码:
use Microsoft\Graph\Graph;
use Microsoft\Graph\Http\GraphRequest;
$tenantId= '<your tenant name/ID>';
$clientId = '<azure ad app id>';
$clientSecret ='<azure ad app secret>';
$targetUser = '<target user UPN/ID>';
$guzzle = new \GuzzleHttp\Client();
$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',
],
])->getBody()->getContents());
$accessToken = $token->access_token;
$data = array(
"GivenName" => "Test2",
"Surname" => "account2",
"EmailAddresses" => array(
array(
"Address" => "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="691d0c1a1d080a0a061c071d5b2904080005470a0604" rel="noreferrer noopener nofollow">[email protected]</a>",
"Name" => "Mail Name2"
)
)
);
$graph = new Graph();
$graph->setAccessToken($accessToken);
$graph->createRequest("POST", "/users/$targetUser/contacts")
->addHeaders(array("Content-Type" => "application/json"))
->attachBody($data)
->execute();
关于php - MS Graph php 创建联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66196204/