Twitter API 在调用 statuses/update 时以 "Your credentials do not allow access to this resource" 响应。json

Twitter API responds with "Your credentials do not allow access to this resource" while calling statuses/update.json

我在我的 PHP 应用程序中使用 Hybridauth 3 代表我的帐户定期发布一些推文。 该应用程序拥有所有可能的权限。当它在第一个身份验证步骤中要求它们时,我会授予它所有权限。 之后 Twitter 将我重定向到指定的回调 URL,我在那里得到了一对 access_token 和 access_token_secret。 但是当我尝试使用这些标记发推文时 - 它给了我:

{"errors":[{"code":220,"message":"Your credentials do not allow access to this resource."}]} 

以下是我尝试发推文的方式:

$config = [
    'authentication_parameters' => [
    //Location where to redirect users once they authenticate 
    'callback' => 'https://mysite/twittercallback/',
    //Twitter application credentials
    'keys' => [
            'key'     => 'xxx', 
            'secret' => 'yyy' 
    ],
    'authorize' => true
    ]
];

$adapter = new Hybridauth\Provider\Twitter($config['authentication_parameters']);

//Attempt to authenticate the user 
$adapter->setAccessToken(/*tokens I've got from getAccessToken() on /twittercallback/*/);

if(! $adapter->isConnected()) {
    // never goes here, so adapter is connected
    return null;
}

try{  
    $response = $adapter->setUserStatus('Hello world!');
}
catch (\Exception $e) {
    // here I've got the error
    echo $e->getMessage();
    return;
}

尝试重新创建令牌和 key\secret 对并多次通过应用程序的身份验证过程,包括为我的 Twitter 帐户输入密码(如 Whosebug 上的一些帖子中所建议的那样),但仍然出现此错误。

P.S。根据 ,Hybridauth 已在最近的版本中修复了该问题。

您似乎使用的是应用程序身份验证而不是用户身份验证。为了 post 推文,您必须验证为用户。此外,请确保您的 Twitter 应用程序具有 read/write 权限。

将来自我的服务器的 header 传出请求与 required by Twitter 的请求进行比较后,我注意到 Hybris 没有添加 header 中非常重要的部分:oauth_token。至少它没有在 Twitter 适配器的代码中以及在使用 setAccessToken() 应用访问令牌时的场景中这样做。它只是将令牌存储在内部存储中,而不是初始化 OAuth1 class 中名为 consumerToken 的相应 class 成员。 因此,为了正确初始化消费者令牌,我覆盖了 Twitter class 的 apiRequest 方法(在它使用默认 parent 实现之前)并添加了一个小条件,所以当消费者令牌在请求之前为空 - 我们需要尝试初始化它。

public function apiRequest($url, $method = 'GET', $parameters = [], $headers = [])
    {
        if(empty($this->consumerToken)) {
            $this->initialize();
        }

        return parent::apiRequest($url, $method, $parameters, $headers);
    }

我不确定我是否以最好的方式修复了它,但只要它能正常工作就可以了。

关于您的信息 setAccessToken 已在 v3.0.0-beta.2 中修复(参见 PR https://github.com/hybridauth/hybridauth/pull/880

我在实施 sample app in clojure and the following resource was a huge help to sort out my confusion about application-only auth vs user authentication: https://developer.twitter.com/en/docs/basics/authentication/overview/oauth

时遇到了同样的错误