1

We are implementing the Orange business apps API. For this, we need to call an Orange service. This service has Bearer type authentication (Oauth2). I am able to get access token successfully and access token validity is 90 days. By using latest access token, I tried to call an Orange service by sending the access token in header but getting unauthorized access error every time.

Here is code

public static function sendEventProductOrder($eventProductOrdering) {

        $jsonMapper = new JsonMapper();

        $opts = array('http' => array('method'=>'POST',
                                        'header'=>'Content-type: application/x-www-form-urlencoded\r\n'.'Authorization: Bearer xxxxxxxxxxxxxxxxx\r\n',
                                        'content' => json_encode($jsonMapper->unmap($eventProductOrdering)),

        ));

        $context = stream_context_create($opts);

        if (($stream = fopen("https://api.orange.com/mba/productordering/v2/event", 'r', false, $context)) !== false) {
            $content = stream_get_contents($stream);
            $header = stream_get_meta_data($stream);
            fclose($stream);
            return HelperMisc::isEqualIgnoreCase($header['wrapper_data']['0'], 'HTTP/1.1 201 Created');
        }

        return false;
    }

Can any one help me in this?

Thank you for your help!

user3616545
  • 90
  • 10

1 Answers1

1

Can you try to change quote ' to "? Like this:

$requestArray = [
    'name' => 'User',
    'age'  => 100
];

'header'  => "Content-type: application/x-www-form-urlencoded\r\n" .
             "Authorization: Bearer xxxxxxxxxxxxxxxxx\r\n",
'content' => http_build_query($requestArray)

Because \r\n doesn't work inside one quote ', it sends 'as is'

Vasyl Zhuryk
  • 1,228
  • 10
  • 23
  • Thanks for your response. I tried it by changing single quotes to double quotes then Error changed to 400 bad request (Earlier error was 401 unauthorized). Do have any idea what is wrong here? – user3616545 Sep 24 '18 at 09:55
  • @user3616545 Try this `'content' => http_build_query($jsonMapper->unmap($eventProductOrdering))` or May you show dump of $opts? – Vasyl Zhuryk Sep 24 '18 at 16:35
  • I had issue in json request that I missed some keys in json , so they sent 400 Bad request. After reaching support, they pointed me with missing parameters. I updated it and it is working now. You pointed the error in code. Thank you for your help!. – user3616545 Sep 26 '18 at 07:48