drudfe0446838 2016-01-18 09:57 采纳率: 0%
浏览 74
已采纳

使用php在MS Dynamics中使用访问令牌访问web api

For Previous question about Azure account, I can create an app using azure account.

Now I can get auth code from below url: Auth_code

From Auth_code we can get the access token by:

  $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. 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.

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.

Web api reference url : reference for web api url

  • 写回答

1条回答 默认 最新

  • duan205571 2016-01-19 05:56
    关注

    Refer to the guide Compose HTTP requests and handle errors, as the requirements shown at HTTP headers section:

    Every request should include the Accept header value of application/json, even when no response body is expected.

    And there are supernumerary blank in your PHP script at curl_setopt($curl, CURLOPT_HTTPHEADER, array($authHeader, 'Content- Type:application/json')); You can remove the blanks in Content-Type and try again.

    By the way, you can leverage Fiddler to capture the requests from your PHP client. 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. If the code is always 200 without response body, you may check your code in web api.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来