0

I'm trying to authenticate my application with the ETrade API using OAuth. After I redirect the browser to ETrade for the user to authenticate, the callback URL is the same page the user started with (I go back to the same page in order to finish off the authentication). However, once it's redirected back to the same page, the redirect to ETrade happens again. How would I do this so the app isn't redirected but rather can finish off the authentication? Thanks!

PHP:

require_once("./autotrading/SDK/samples/config.php");
require_once("./autotrading/SDK/Common/common.php");    
  $key = ETWS_APP_KEY;
  $secret = ETWS_APP_SECRET;

  $consumer = new etOAuthConsumer($key, $secret); 
  $request = new etOAuth($consumer);

  $req_token = $request->GetRequestToken(); 
  $oauthToken = $req_token['oauth_token'];
  $oauthSecret = $req_token['oauth_token_secret'];
  $authURL = $request->GetAuthorizeURL(); 
  header('Location: '.$authURL);
//after authenticating, etrade's callback url looks like this: http://yourdomain.com/index.php?oauth_token=abc&oauth_verifier=123
if(isset($_GET['oauth_verifier'])) {
  $verifierCode = trim($_GET['oauth_verifier']);
  echo $verifierCode;
  $accessCode = GetAccessToken($verifierCode);
}

For reference, here's the API: https://developer.etrade.com/ctnt/dev-portal/getContent?contentUri=V0_Code-SDKGuides-PHP

user3781239
  • 131
  • 3
  • 11

2 Answers2

0

The problem is when the callback comes and runs this it never reaches your if statment at the end because you have a header being set. Try this...

if(isset($_GET['oauth_verifier'])) {
  $verifierCode = trim($_GET['oauth_verifier']);
  echo $verifierCode;
  $accessCode = GetAccessToken($verifierCode);
}else{
  header('Location: '.$authURL); 
}
Brént Russęll
  • 476
  • 4
  • 12
-1

If your callback with etrade is configured properly, you should be getting oauth_verifier in the query string.

You should be able to trap that oauth_verifier query string element exists and has a value.

Look at page 16 of the etrade PDF concerning their API.

Gyrocode.com
  • 51,125
  • 13
  • 124
  • 164
  • He is traping oauth_verifier but his logic is not correct because he has a header location set before he traps it. See my answer. – Brént Russęll Jul 04 '18 at 09:38