代码之家  ›  专栏  ›  技术社区  ›  Masinde Muliro

在Azure AD上创建架构扩展遇到挑战

  •  0
  • Masinde Muliro  · 技术社区  · 7 年前

    我正在使用以下函数在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;
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Philippe Signoret    7 年前

    代码的主要*问题是缺少必需的 properties 领域

    如中所述 Microsoft Graph doc on creating schema extensions ,您必须提供 id ,则, description ,则, targetTypes 属性 创建新 schemaExtension 。(The owner 属性是可选的。)

    以下代码应该可以工作。(请注意,我还切换到使用v1.0端点,因为这已经超出了beta版。)

    $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();
    

    *失踪者 属性 财产是主要问题,但你还有几个其他问题:

    • 您正在尝试引用 id号 具有的属性 $arr->id 但是 json_decode($string, true) 返回关联数组,以便使用 $arr['id'] 相反
    • 您正在尝试设置 status 创建过程中的架构扩展,这也会导致错误(尽管是另一个错误)。首次创建架构扩展时,请忽略 地位 字段,并在准备就绪后进行更新。