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;

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.

    解决 无用
    打赏 举报

相关推荐 更多相似问题