简体   繁体   English

使用PHP在MS Dynamics中使用访问令牌访问Web API

[英]Access web api using access token in MS Dynamics using php

For Previous question about Azure account , I can create an app using azure account. 对于有关Azure帐户的上一个问题 ,我可以使用azure帐户创建一个应用。

Now I can get auth code from below url: Auth_code 现在,我可以从以下网址获取身份验证代码: Auth_code

From Auth_code we can get the access token by: 从Auth_code中,我们可以通过以下方式获取访问令牌:

  $auth_code = $_GET['code'];
  $result = access($auth_code);


   function access($auth_code){
        $redirectUri = 'https://XXXX /authorize.php';


    $token_request_data = array (
    "grant_type" => "authorization_code",
    "code" => $auth_code,
    "redirect_uri" => $redirectUri,
    "client_id" => "client_id",
    "client_secret" => "client_secret",
    "resource" =>"resource" (From manifest in azure)
  );


  $token_request_body = http_build_query ( $token_request_data );


   $curl = curl_init ( 'https://login.windows.net/common/oauth2/token' );
   curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, true );
   curl_setopt ( $curl, CURLOPT_POST, true );
   curl_setopt ( $curl, CURLOPT_POSTFIELDS, $token_request_body );
   curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER,false);

    $response = curl_exec ( $curl );

    $res = json_decode($response);
    curl_close ( $curl );

Now I'm trying to access the web api using that access_token,I couldn't get the result. 现在,我尝试使用该access_token访问Web api,但无法获得结果。 For example: 例如:

 $authHeader = 'Authorization:Bearer access_toke';
 $ch = curl_init();
 $url = 'https://domain/api/data/v8.0/contacts';
 $curl = curl_init();
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
 curl_setopt($curl, CURLOPT_POST, false);
 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($curl, CURLOPT_URL, $url);
 curl_setopt($curl, CURLOPT_HTTPHEADER, array($authHeader, 'Content-  Type:application/json'));
 $result = curl_exec($curl);
  echo "<pre>";print_r($result);exit;
 curl_close($curl);

I'm getting empty response. 我的回应是空的。 Now I have to know how to access the web API using access token. 现在,我必须知道如何使用访问令牌访问Web API。

When I try to run manually https://domain/api/data/v8.0/contacts , I can get all contacts in my crm.But when I try to access it by access_token using php,it returns empty. 当我尝试手动运行https://domain/api/data/v8.0/contacts ,我可以在我的crm中获取所有联系人。但是当我尝试使用php通过access_token访问它时,它返回为空。

Web api reference url : reference for web api url Web API参考URLWeb API URL的 参考

Refer to the guide Compose HTTP requests and handle errors , as the requirements shown at HTTP headers section: 请参阅指南撰写HTTP请求和处理错误 ,如HTTP标头部分所示的要求:

Every request should include the Accept header value of application/json, even when no response body is expected. 每个请求都应包括application / json的Accept标头值,即使没有响应主体也是如此。

And there are supernumerary blank in your PHP script at curl_setopt($curl, CURLOPT_HTTPHEADER, array($authHeader, 'Content- Type:application/json')); 在PHP脚本中, curl_setopt($curl, CURLOPT_HTTPHEADER, array($authHeader, 'Content- Type:application/json')); You can remove the blanks in Content-Type and try again. 您可以删除Content-Type中的空格,然后重试。

By the way, you can leverage Fiddler to capture the requests from your PHP client. 顺便说一下,您可以利用Fiddler捕获来自PHP客户端的请求。 We you get the response body content and request status code via the tool. 我们通过该工具获得响应正文内容并请求状态代码。 We can match the status code with the list at https://msdn.microsoft.com/en-us/library/gg334391.aspx#bkmk_statusCodes . 我们可以在https://msdn.microsoft.com/zh-cn/library/gg334391.aspx#bkmk_statusCodes上将状态代码与列表进行匹配。 If the code is always 200 without response body, you may check your code in web api. 如果代码始终为200而没有响应主体,则可以在Web api中检查代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM