TDAmeritrade API Authentication Error: Failed to resolve API Key variable request.header.un
TDAmeritrade API Authentication Error: Failed to resolve API Key variable request.header.un
我在使用 PHP (https://developer.tdameritrade.com/authentication/apis/post/token-0)
从令牌端点获取访问令牌时遇到困难
特别是,我收到以下错误:
{ "error":"Failed to resolve API Key variable request.header.un" }
我的请求,使用 PHP,是:
$url = 'https://api.tdameritrade.com/v1/oauth2/token';
$client_id = $customer_key ;
$redirect_uri = $redirect_URL;
$myvars = array("grant_type" => "authorization_code"
, "access_type" => "offline"
, "client_id" => $client_id
, "redirect_uri" => $redirect_URL
, "code" => "$code");
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $ch, CURLOPT_VERBOSE, 1);
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded; charset=utf-8'
));
$response = curl_exec( $ch );
echo "<br>token = '$response'\n";
}
谢谢!
当我收到完全相同的错误消息时,我找到了这个页面。经过多次尝试并离开几次后,我终于弄明白了。与您发布的内容相比,最大的变化是当您使用 header 时:
"Content-Type: application/x-www-form-urlencoded"
which TD wants for this API, 你需要将 $myvars
数组变成如下字符串:
$myvars = "grant_type=authorization_code&access_type=offline&code=".$code."&client_id=".$client_id."&redirect_uri=".urlencode($redirect_uri);
此处的另一个注意事项是 urlencode 函数,用于帮助 url 对字符串进行编码。它在本页的 'Notes' 部分讨论了这一点:
好的,我终于得到了一个有效的 PHP 脚本,它可以验证进入 TD API 并获取帐户信息。我想与大家分享它,因为在将近三个月后我无法弄清楚如何让它工作。特别感谢 Ninet3 帮助我。您可以直接向他的 Fiverr 个人资料提问 (https://www.fiverr.com/ninety3)
$redirect_URL = 'https://YourURL.com';
$redirect_URL = urlencode($redirect_URL);
$customer_key = 'XXXXXXXXXXXXXXXXXXXX';
$account_number = '1234567890';
$url_endpoint = 'https://auth.tdameritrade.com/auth?response_type=code&redirect_uri='.$redirect_URL. '&client_id='.$customer_key.'%40AMER.OAUTHAP';
if(NULL === @$_GET['code']) {
header("Location: $url_endpoint");//open TD website to get tokens
}
if($_GET['code'] !== '') {
$code = $_GET['code'];
$code = urlencode($code);
if($code){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.tdameritrade.com/v1/oauth2/token',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0, CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded',
),
CURLOPT_POSTFIELDS => 'grant_type=authorization_code&refresh_token=&access_type=offline&code='.$code.'&client_id='.$customer_key.'&redirect_uri='.$redirect_URL,
));
$response = curl_exec($curl);
curl_close($curl);
$response = (json_decode($response, true));
echo '<pre>';
echo 'access_token: <br>';
print_r($response );
//Get account information
if(@$response['access_token'] !== null){
$acc = $account_number;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.tdameritrade.com/v1/accounts/'.$acc,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer '.$response['access_token'],
),
));
$response = curl_exec($curl);
curl_close($curl);
echo '<pre>'.$response;
}else{
echo 'unable to get access token'; exit;
}
}
else{
echo 'No Code Found'; exit;
}
}else{
echo 'No Code Found'; exit;
}
您需要将令牌存储在数据库中,并在时间到期后刷新以获取新令牌。
我在使用 PHP (https://developer.tdameritrade.com/authentication/apis/post/token-0)
从令牌端点获取访问令牌时遇到困难特别是,我收到以下错误:
{ "error":"Failed to resolve API Key variable request.header.un" }
我的请求,使用 PHP,是:
$url = 'https://api.tdameritrade.com/v1/oauth2/token';
$client_id = $customer_key ;
$redirect_uri = $redirect_URL;
$myvars = array("grant_type" => "authorization_code"
, "access_type" => "offline"
, "client_id" => $client_id
, "redirect_uri" => $redirect_URL
, "code" => "$code");
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $ch, CURLOPT_VERBOSE, 1);
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded; charset=utf-8'
));
$response = curl_exec( $ch );
echo "<br>token = '$response'\n";
}
谢谢!
当我收到完全相同的错误消息时,我找到了这个页面。经过多次尝试并离开几次后,我终于弄明白了。与您发布的内容相比,最大的变化是当您使用 header 时:
"Content-Type: application/x-www-form-urlencoded"
which TD wants for this API, 你需要将 $myvars
数组变成如下字符串:
$myvars = "grant_type=authorization_code&access_type=offline&code=".$code."&client_id=".$client_id."&redirect_uri=".urlencode($redirect_uri);
此处的另一个注意事项是 urlencode 函数,用于帮助 url 对字符串进行编码。它在本页的 'Notes' 部分讨论了这一点:
好的,我终于得到了一个有效的 PHP 脚本,它可以验证进入 TD API 并获取帐户信息。我想与大家分享它,因为在将近三个月后我无法弄清楚如何让它工作。特别感谢 Ninet3 帮助我。您可以直接向他的 Fiverr 个人资料提问 (https://www.fiverr.com/ninety3)
$redirect_URL = 'https://YourURL.com';
$redirect_URL = urlencode($redirect_URL);
$customer_key = 'XXXXXXXXXXXXXXXXXXXX';
$account_number = '1234567890';
$url_endpoint = 'https://auth.tdameritrade.com/auth?response_type=code&redirect_uri='.$redirect_URL. '&client_id='.$customer_key.'%40AMER.OAUTHAP';
if(NULL === @$_GET['code']) {
header("Location: $url_endpoint");//open TD website to get tokens
}
if($_GET['code'] !== '') {
$code = $_GET['code'];
$code = urlencode($code);
if($code){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.tdameritrade.com/v1/oauth2/token',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0, CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded',
),
CURLOPT_POSTFIELDS => 'grant_type=authorization_code&refresh_token=&access_type=offline&code='.$code.'&client_id='.$customer_key.'&redirect_uri='.$redirect_URL,
));
$response = curl_exec($curl);
curl_close($curl);
$response = (json_decode($response, true));
echo '<pre>';
echo 'access_token: <br>';
print_r($response );
//Get account information
if(@$response['access_token'] !== null){
$acc = $account_number;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.tdameritrade.com/v1/accounts/'.$acc,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer '.$response['access_token'],
),
));
$response = curl_exec($curl);
curl_close($curl);
echo '<pre>'.$response;
}else{
echo 'unable to get access token'; exit;
}
}
else{
echo 'No Code Found'; exit;
}
}else{
echo 'No Code Found'; exit;
}
您需要将令牌存储在数据库中,并在时间到期后刷新以获取新令牌。