gpt4 book ai didi

php - TDAmeritrade API 认证错误 : Failed to resolve API Key variable request. header.un

转载 作者:行者123 更新时间:2023-12-04 14:15:58 28 4
gpt4 key购买 nike

我在使用 PHP ( https://developer.tdameritrade.com/authentication/apis/post/token-0 ) 从 token 端点获取访问 token 时遇到困难

特别是,我收到以下错误:

{ "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";

}

谢谢!

最佳答案

好的,我终于得到了一个有效的 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;
}

您需要将 token 存储在数据库中,并在时间到期后刷新以获取新 token 。

关于php - TDAmeritrade API 认证错误 : Failed to resolve API Key variable request. header.un,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60318672/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com