0

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 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.