【问题标题】:Twitter Ads API error: INSUFFICIENT_USER_AUTHORIZED_PERMISSION. How to solve it?Twitter 广告 API 错误:INSUFFICIENT_USER_AUTHORIZED_PERMISSION。如何解决?
【发布时间】:2019-09-21 10:14:06
【问题描述】:

我正在尝试在我的开发环境中执行对 twitter 广告 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 准备好查询广告 API 的原因。

除了我在页面https://developer.twitter.com/en/apps 中有关于应用程序(令牌和秘密)的信息,但我找不到对account_id 的任何引用,在official documentation 中提到。

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 之后,我创建了以下代码以访问 Twitter 广告 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":{}}}

我错过了什么?

【问题讨论】:

    标签: api twitter twitter-oauth ads


    【解决方案1】:

    我找到了解决办法。我不知道这是否是唯一的,但它有效。

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

    1. 在您的系统中安装 twurl。 $ sudo gem install twurl
    2. 设置授权 twurl 访问您的推特 APP。 $ twurl authorize --consumer-key xxxxx --consumer-secret xxxxx
    3. 这是前面命令的输出: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. 重新生成后,您可以在终端中访问 api 槽 twurl:$ twurl -H "ads-api.twitter.com" "/5/accounts"。请注意,今天(2019 年 5 月)我在 "/5/accounts" 中使用数字 5。您必须在您的日期检查您的版本。
    11. 现在您还可以通过 php 中的 curl 访问 Twitter 广告 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);
    

    【讨论】:

    • 我可以确认这种方法到今天(2020 年)仍然有效。只需将 /5/accounts 更改为 /8/accounts 以匹配当前的 API 版本 (8)。我不能 100% 确定是否有必要重新生成令牌:完成 PIN 身份验证后,我就能够使用 twurl 工具对 /8/accounts 进行 API 调用。 YMMV。我仍然无法让它与 PostMan 一起使用,但我相信这无关紧要,因为我的密钥现在可以与 Python SDK 和 twurl 工具一起使用。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-29
      • 1970-01-01
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      • 1970-01-01
      • 2020-06-04
      相关资源
      最近更新 更多