我正在尝试使用仅应用程序的身份验证,如下所述:
https://developer.twitter.com/en/docs/basics/authentication/overview/application-only
我正在使用下面的PHP代码来执行此操作。
if(空($cookie['twitter_auth'])){
需要'../../social_audit_config/twitter_config.php';
$encoded_key=urlencode($api_key);
$encoded_secret=urlencode($api_secret);
$credentials=$encoded_key.:“”$encoded_secret;
$encoded_credentials=base64_encode($credentials);
$request_headers=数组(
'主机:api.twitter.com',
'用户代理:bf共享报告',
'授权:基本'.$encoded_credentials,
'内容类型:application/x-www-form-urlencoded;charset=utf-8',
'内容长度:29',
'接受编码:gzip'
)(二)
打印_r($request_headers);
$ch=curl_init();
curl_setopt($ch,curlot_customrequest,“post”);
curl_setopt($ch,curlot_httpheader,$request_headers);
curl_setopt($ch,curlot_returnTransfer,true);
curl_setopt($ch,curlpt_ssl_verifypeer,false);
curl_setopt($ch,curlot_url,'https://api.twitter.com/oauth2/token');
curl_setopt($ch,curlot_postfields,'grant_type=client_credentials');
$attempt_auth=curl_exec($ch);
打印($attempt\u auth);
}
它应该返回带有令牌的JSON,但是它返回GobbledyGook,如下图所示:
我肯定我错过了一些非常简单的步骤,我哪里出错了?
如果我发送没有头的curl请求,它将按预期返回JSON格式的错误,那么我的头有什么问题吗?
我使用下面的PHP代码来实现这一点。
if(empty($_COOKIE['twitter_auth'])) {
require '../../social_audit_config/twitter_config.php';
$encoded_key = urlencode($api_key);
$encoded_secret = urlencode($api_secret);
$credentials = $encoded_key.":".$encoded_secret;
$encoded_credentials = base64_encode($credentials);
$request_headers = array(
'Host: api.twitter.com',
'User-Agent: BF Sharing Report',
'Authorization: Basic '.$encoded_credentials,
'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
'Content-Length: 29',
'Accept-Encoding: gzip'
);
print_r($request_headers);
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, 'https://api.twitter.com/oauth2/token');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
$attempt_auth = curl_exec($ch);
print_r($attempt_auth);
}
它应该返回带有令牌的JSON,但是它返回gobbledygook,如下图所示:
我肯定我错过了一些非常简单的步骤,我哪里出错了?
如果我发送没有头的curl请求,它将按预期返回JSON格式的错误,那么我的头有什么问题吗?