我正在尝试在我的开发环境中对twitter Ads API执行请求。我已经注册可以使用此服务。
我收到了这样的确认电子邮件:
Your application (ID:12345678) has been approved for the Twitter Ads API program and your organization has been granted a Developer license for Read/Write access. ...
这就是为什么我想准备好我的APP来查询Ads API的原因。
此外,我在页面https://developer.twitter.com/en/apps
中有关于APP的信息(令牌和秘密),但找不到official documentation中提到的对account_id
的引用。 / p>
Advertising accounts are registered on ads.twitter.com and identified
in the API by account_id. Advertising accounts link directly to funding
sources and leverage content from one or more Twitter user accounts as
‘promotable users’. Each advertising account can grant permission to
one or more Twitter user accounts. The advertising account, or “current
account,” is represented in nearly every URL executed as an in-line
:account_id parameter.
在此post之后,我已经在oder中创建了以下代码来访问Twitter Ads API:
$settings = array(
'oauth_access_token' => env('TWITTER_ACCESS_TOKEN'),
'oauth_access_token_secret' => env('TWITTER_ACCESS_TOKEN_SECRET'),
'consumer_key' => env('TWITTER_CONSUMER_KEY'),
'consumer_secret' => env('TWITTER_CONSUMER_SECRET'),
);
$url = 'https://api.twitter.com/1.1/followers/ids.json';
$getfield = '?screen_name=J7mbo';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchangeService($settings);
$data = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
dd($data);
上一个代码有效(我不查询Ads API。但是下一个代码(查询Ads Api)无效:
$url = 'https://ads-api.twitter.com/5/accounts';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchangeService($settings);
$data = $twitter->buildOauth($url, $requestMethod)->performRequest();
dd($data);
{"errors":[{"code":"INSUFFICIENT_USER_AUTHORIZED_PERMISSION","message":"User 2222222222 is not authorized to make this request. Please have them reauthorize with your client application APPNAme."}],"request":{"params":{}}}
我想念什么?
答案 0 :(得分:0)
我找到了解决方案。我不知道这是否是唯一的一个,但它可以工作。
我们必须安装Twurl。 Twurl是一个类似curl的应用程序,专门为Twitter API量身定制。
$ sudo gem install twurl
$ twurl authorize --consumer-key xxxxx --consumer-secret xxxxx
Go to https://api.twitter.com/oauth/authorize?oauth_consumer_key=xxxx&oauth_nonce=ffff&oauth_signature=bbb&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1556889574&oauth_token=ddd&oauth_version=1.0 and paste in the supplied PIN
https://api. .... version=1.0
'You've granted access to APP_Name! Next, return to APP_Name and enter this PIN to complete the authorization process. PIN = 09010101'
。Authorization successful
中收到一条消息。https://developer.twitter.com/en/apps/123456
,然后转到Keys and tokens
部分。您需要重新生成访问令牌和访问令牌密钥。点击按钮'regenerate'
$ twurl -H "ads-api.twitter.com" "/5/accounts"
中访问api槽twurl。请注意,今天(2019年5月)我正在使用"/5/accounts"
中的数字5。您必须在日期检查您的版本。TwitterAPIExchangeService
(我在Laravel 5.8中)。您可以在此post中上课。将以下代码与您的密钥一起使用:
$settings = array(
'oauth_access_token' => env('TWITTER_ACCESS_TOKEN'),
'oauth_access_token_secret' => env('TWITTER_ACCESS_TOKEN_SECRET'),
'consumer_key' => env('TWITTER_CONSUMER_KEY'),
'consumer_secret' => env('TWITTER_CONSUMER_SECRET'),
);
//Regular Twitter API
$url = 'https://api.twitter.com/1.1/followers/ids.json';
$getfield = '?screen_name=J7mbo';
//Ads Twitter API
//$url = 'https://ads-api.twitter.com/5/accounts';
//$getfield = '';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchangeService($settings);
$data = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
dd($data);
答案 1 :(得分:0)
需要重新生成密钥和令牌。在那之后对我有用