无法使用PHP和Curl连接到Microsoft Dynamics CRM [英] Unable to connect to Microsoft Dynamics CRM with PHP and Curl

查看:82
本文介绍了无法使用PHP和Curl连接到Microsoft Dynamics CRM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP和CURL连接到Microsoft Dynamics API,以便可以从CRM读取客户端数据.可以在以下位置找到API指南: https://msdn.microsoft.com/en-gb/library/mt593051.aspx

I am trying to use PHP and CURL to connect to Microsoft Dynamics API so I can read client data from the CRM. The API guide can be found here: https://msdn.microsoft.com/en-gb/library/mt593051.aspx

我已经进入Azure门户并设置了一个新应用程序,它为我提供了使用凭据(客户端ID,机密等)和url端点.使用这些凭据,我能够成功连接到CRM并检索承载访问令牌,但是我无法获得进一步的信息.

I've been into the Azure portal and set up a new application, and it gives me the credentials to use (client id, secret, etc.) and the url end points. Using these credentials I am able to successfully connect to the CRM and retrieve a bearer access token, but I am unable to get any further.

当我尝试使用令牌返回数据时,我收到以下错误消息:

When I attempt to use the token to return data I receive the below error message:

HTTP错误401-未经授权:访问被拒绝

HTTP Error 401 - Unauthorized: Access is denied

我的假设是我必须正确传递令牌?

My assumption would be that I must be passing the token correctly?

我的代码在下面.

<?php
// Step 1 - Use the credentials supplied by CRM to get an access token (this bit works okay)
$credentials = array(
    'grant_type'=>'client_credentials',
    'username'=>'xxxxxxxx',
    'password'=>'xxxxxxxx',
    'client_id'=>'xxxxxxxxxxxx',
    'client_secret'=>'xxxxxxxxxx',
    );
$urlSafeCredentials = http_build_query($credentials);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://login.microsoftonline.com/xxxxxxxxxxxxxx/oauth2/token');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $urlSafeCredentials);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); 
$response = curl_exec($ch);
$result = json_decode($response);
curl_close($ch);


// A BEARER access token is successfully returned
$token = $result->access_token;


// Step 2 - Use the access token to request data from the CRM (this bit fails with HTTP Error 401 - Unauthorized: Access is denied)

$ch = curl_init('https://clientspecificurl.crm4.dynamics.com/api/data/v8.1/accounts');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/x-www-form-urlencoded','Authorization: Bearer '.$token)); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
print_r($response); // 401 Unauthorized ?!
?>  

据我所知,在后端没有其他可配置的内容,我们将不胜感激.

As far as I can tell there is nothing else to configure at the back end, any help would be much appreciated.

推荐答案

我编写了一个轻量级PHP类,用于使用Dynamics 365在线Web API.您可以在此处找到它.

I wrote a lightweight PHP class for working with Dynamics 365 online Web API. You can find it here.

顺便说一句,在代码中,您应该尝试在grant_type内尝试使用"password",而不是"client_credentials"

BTW in your code you should try "password" inside the grant_type instead of "client_credentials"

这篇关于无法使用PHP和Curl连接到Microsoft Dynamics CRM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆