Oracle APEX oAuth : Unable to get the Access Token / Authorization Code from 2nd Time onwards

Viewed 1701

I am trying to get oAuth token from Oracle Apex using Authorization Code ProcessFlow.

Refer: http://www.oracle.com/technetwork/developer-tools/rest-data-services/documentation/listener-dev-guide-1979546.html#acquiring_a_token_using_the_authorization_code_protocol_flow

Steps I did :

1) Registered the oAuth Client in Apex with redirect URI and obtained the

Client ID : MY_CLIENT_ID
Client Secret : MY_CLIENT_SECRET
Authorization URI : https://apex.oracle.com/pls/apex/MY_WORK/oauth2/auth?response_type=code&client_id= MY_CLIENT_ID&client_secret= MY_CLIENT_SECRET &state=_replace_with_a_unique_value_

2) When I opened the Authorization URI in my web browser and clicked “Allow Access” I ended up getting the Authorization Code in the URL:

https://apex.oracle.com/pls/apex/MY_WORK/hr/?code=MY_AUTH_CODE&state=a19b7f7c-c31d-9c79-3f5c-bf6893a03552

3) I created a PHP page(http://server.com/oauth/index.php) in web host and requested access token from APEX server using cURL with following parameters Client ID : MY_CLIENT_ID Client Secret : MY_CLIENT_SECRET Authorization Code : MY_AUTH_CODE URI : https://apex.oracle.com/pls/apex/MY_WORK/oauth2/token

PHP Code :

<?php

 //actual cURL
 /*curl -i -d "grant_type=authorization_code&code= MY_CLIENT_ID " \
 --user MY_CLIENT_SECRET : MY_AUTH_CODE \
 https://apex.oracle.com/pls/apex/MY_WORK/oauth2/token*/

 $pageurl = "https://apex.oracle.com/pls/apex/MY_WORK/oauth2/token";
 $ch = curl_init($pageurl);
 curl_setopt($ch, CURLOPT_HEADER, TRUE); // -i
 curl_setopt($ch,CURLOPT_POST, 2);
 curl_setopt($ch,CURLOPT_POSTFIELDS, "grant_type=authorization_code&code=  MY_CLIENT_ID "); // -d
 curl_setopt($ch,CURLOPT_USERPWD, " MY_CLIENT_SECRET : MY_AUTH_CODE "); // --user
 $resp = curl_exec($ch);
 curl_close($ch);
 echo $resp;
 ?>

4) When I visited the PHP URL http://server.com/oauth/index.php I ended up getting the access token in JSON :

 {"access_token":"MY_ACCESS_TOKEN","token_type":"bearer","expires_in":36000,"refresh_token":" MY_REFRESH_ACCESS_TOKEN "}1

So I was able to get the access token successfully but the issue is when I try to get the ACCESS_TOKEN second time by opening the PHP URL http://server.com/oauth/index.php.

I ended up getting the Invalid grant : {"error":"invalid_grant"}1

So I decided to update the new Authorization Code in PHP, which I should get by accessing the Authorization URI as in step 2, but I ended up in 400-Bad Request.

But if do everything from the 1st step, its working fine but ended up with same issue in 2nd request.

As of my application I just wanted to perform step 4 again and again to get new tokens, which is causing the above issue.

So finally I wonder why I am not able to get 1. the Access Token 2nd time using the Authorization Code as in step 3 2. and the Authorization Code 2nd time using Authorization URI as in step

Please give me some suggestion to get ACCESS_TOKEN from Oracle APEX programmatically.

0 Answers
Related