我使用以下函数在Azure AD上创建架构扩展,但我最终得到以下错误:
{
"code":"InternalServerError",
"message":"Object reference not set to an instance of an object."
}
以下是我的功能:
public function createExtension($id, $description, $targetTypes) {
$token = $this - > getToken();
// $graph = new Graph();
// $graph->setAccessToken($token);
$headers = [
'Authorization: Bearer '.$token,
'Content-Type: application/json'
];
$data = json_encode([
'id' => $id,
'description' => $description,
'targetTypes' => ["User"],
'status' => 'Available'
]);
$ch = curl_init('https://graph.microsoft.com/beta/schemaExtensions');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$arr = json_decode(curl_exec($ch), true);
$logger = $this - > container - > get('monolog.logger.azure');
$logger - > info('Values', ['ch' => $arr]);
return $arr - > id;
}
答案 0 :(得分:0)
您的代码的主要*问题是您缺少必需的properties
字段。
正如Microsoft Graph doc on creating schema extensions中所述,您必须在创建新id
时提供description
,targetTypes
,properties
和schemaExtension
。 (owner
属性是可选的。)
以下代码应该有效。 (请注意,我已经切换到使用v1.0端点,因为它已经没有测试版了。)
$token = $this->getToken(); // Assume this does what the name implies
$headers = [
'Authorization: Bearer ' . $token,
'Content-Type: application/json'
];
$data = json_encode([
'id' => 'size',
'description' => 'Height and shoe size',
'targetTypes' => ['User'],
'properties' => [
[
'id' => 'shoeSize',
'type' => 'Integer',
],
[
'id' => 'height',
'type' => 'Integer',
],
[
'id' => 'tShirtSize',
'type' => 'String',
],
]
]);
$ch = curl_init('https://graph.microsoft.com/v1.0/schemaExtensions');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$schemaExtension = json_decode(curl_exec($ch));
return $schemaExtension->id;
如果您想使用Microsoft Graph SDK for PHP,代码看起来非常相似:
use Microsoft\Graph\Graph;
use Microsoft\Graph\Model;
$token = $this->getToken(); // Assume this does what the name implies
$graph = new Graph();
$graph->setAccessToken($token);
$newSchemaExtension = new Model\SchemaExtension([
'id' => 'sizes',
'description' => 'Height, shoe and shirt size',
'targetTypes' => ['User'],
'properties' => [
[
'name' => 'shoeSize',
'type' => 'Integer',
],
[
'name' => 'height',
'type' => 'Integer',
],
[
'name' => 'shirtSize',
'type' => 'String',
],
]
]);
$schemaExtension = $graph->createRequest('POST', '/schemaExtensions')
->attachBody($newSchemaExtension)
->setReturnType(Model\SchemaExtension::class)
->execute();
return $schemaExtension->getId();
*缺少properties
属性是主要问题,但您还有其他一些问题:
id
引用$arr->id
属性,但json_decode($string, true)
会返回关联数组,因此您将使用$arr['id']
代替。status
,这也会导致错误(尽管它是不同的错误)。首次创建架构扩展时,请省略status
字段,并在准备好后再更新。