Twitter Ads API错误:INSUFFICIENT_USER_AUTHORIZED_PERMISSION。怎么解决呢?

时间:2019-05-02 20:52:17

标签: api twitter twitter-oauth ads

我正在尝试在我的开发环境中对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":{}}}

我想念什么?

2 个答案:

答案 0 :(得分:0)

我找到了解决方案。我不知道这是否是唯一的一个,但它可以工作。

我们必须安装Twurl。 Twurl是一个类似curl的应用程序,专门为Twitter API量身定制。

  1. 在系统中安装twurl。 $ sudo gem install twurl
  2. 设置授权以转推访问您的Twitter APP。 $ twurl authorize --consumer-key xxxxx --consumer-secret xxxxx
  3. 这是prevoius命令的输出: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
  4. 打开浏览器副本并粘贴提供的URL https://api. .... version=1.0
  5. 您将被重定向到要求确认授权的页面。确认。
  6. 您将收到一条消息:'You've granted access to APP_Name! Next, return to APP_Name and enter this PIN to complete the authorization process. PIN = 09010101'
  7. 只需复制PIN码并将其粘贴回终端,然后按Enter键即可。
  8. 您将在终端Authorization successful中收到一条消息。
  9. 转到APP_Name页面https://developer.twitter.com/en/apps/123456,然后转到Keys and tokens部分。您需要重新生成访问令牌和访问令牌密钥。点击按钮'regenerate'
  10. 一旦重新生成,您就可以在终端$ twurl -H "ads-api.twitter.com" "/5/accounts"中访问api槽twurl。请注意,今天(2019年5月)我正在使用"/5/accounts"中的数字5。您必须在日期检查您的版本。
  11. 现在,您还可以在php中访问Twitter Ads API槽卷曲。
  12. 创建一个类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)

需要重新生成密钥和令牌。在那之后对我有用