Twitter PHP REST API giving {"errors":[{"code":32,"message":"Could not authenticate you."}]}

oauth
search

#1

Hi
I’m have Integrated Twitter Standard search API with in the application.

For some keywords, Initially Search API returned results normally for some days and then throwing following error.

{“errors”:[{“code”:32,“message”:“Could not authenticate you.”}]}

I have checked if the token has expired, but its not. I could do other activity with the same token.

Having issue sometimes is quite confusing.

Can anyone please help me out?

Thanks in advance


#2

It sounds like something might be changing in your request authorization, how are you generating the Oauth details?


#3

Hey,

Thanks for the reply.

We are using Zend_Service_Twitter library for the version Zend 1.12

I have used following code for the reference.

            $twitter_accessToken  // We will fetch from the Twitter after User authentication using OAuth
            $token = unserialize($twitter_accessToken);

            $twitter = Api_Twitter::createTwitterObject($token);
			
            $searchText = $keyword;
	    $searchText = $searchText.' -'.$exclude;  // content to Exclude
	    $searchText = $searchText.'  filter:Images'; // To filter	 images/videos etc

            $options['count'] = '100';
            $options['tweet_mode'] = 'extended';
            if($language != 'any') {
                $options['lang'] = $language;
            }

            $resultData = $twitter->searchTweets($searchText, $options);
            $decodedData = json_decode($resultData->getRawResponse(), true);

For end users, we provide option to add multiple Twitter networks with in our application.

So while adding the column to search, along with filters, we provide an option to choose from which Twitter Network the search to be executed.

The same code working for other Twitter Tokens (for other columns). But for some specific tokens, that too after some iterations, this error message coming. Then after, its stops pulling the data from API, for that Twitter Token.

Couldn’t get the exact scenario why this is happening.


#4

Hi
I didn’t hear any solution on this in mean time.
Did anyone faced similar issue or any solution for the same?
Thanks in advance.


#5

Hi,

It looks like you’re not authenticating with Zend_Oauth as described in https://framework.zend.com/manual/1.12/en/zend.oauth.introduction.html

What does $acc = new Zend_Oauth_Token_Access(); do?

I’m not a PHP Developer so maybe I’m missing something here, but the 32 error is thrown when badly authenticating to Oauth.


#6

Hi

Thank you for the reply.

I have followed the OAuth pattern as mentioned in Twitter library for Zend in GitHub in Following link.

The same pattern worked for fetching results, when searched for a keyword in the first iteration.

On pulling data using next cursors, after some iterations, that too at some times this error is being thrown.


#7

VKusumba,
I’m at work right now and I don’t have access to Github :sweat_smile: … I’ll check on your code later today and hopefully I’ll have something to reply.


#8

Hi Guys,
I thought, there could be some issue with my coding or issue in requesting format, so debugged the flow but couldn’t find any. That too same code is working fine for few days.

Then I planned Switching library could fix this.

From following Twitter mentioned Libraries link.
https://developer.twitter.com/en/docs/developer-utilities/twitter-libraries

I have chosen below mentioned PHP library and integrated it with in the application.

For most of the Twitter access tokens it is working fine (Still working with no errors) similar to the previous library.

But, only for some user tokens (That are not expired, we could fetch timeline data with it), that too after pulling data for 2 to 3 days successfully, it is throwing Could not authenticate you error.

[errors] => Array
        (
            [0] => Array
                (
                    [code] => 32
                    [message] => Could not authenticate you.
                )

        )

This could be a rare scenario. Does anyone faced similar kind of issue?

Thanks in advance.


#9

Is the failing search request always failing for the specific users? If you perform that exact same search with different users does it succeed?


#10

Hi abraham,

Thank you for replying,

No, It is happening randomly for random access tokens.

I tried the same keyword with other access token by applying same filters, It worked and is still pulling data with no errors.

For the same access token also, initially for some days it pulled data successfully, Later stopped by throwing error for that search keyword…

I also checked If the same access token (With out refreshing the token) used for searching different keyword results after stopped fetching, in this scenario also it worked successfully with no errors.


#11

Guys,

Anyone found solution to this kind of rare scenario issue?

Happy to hear form you.

Thanks in advance.


#12

Are the keys and tokens being sent as an array or as query style key/value pairs (key1=val1&key2=val2)? If the content-type is sometimes being set to multipart/form, then you might be getting rejected on that first “incomplete” request.


#13

As @kvk2309 mentioned in previous reply, following is the sample pattern used to get search data.

Example:

$url = 'https://api.twitter.com/1.1/followers/ids.json';
$getfield = '?screen_name=J7mbo';
$requestMethod = 'GET';

$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
    ->buildOauth($url, $requestMethod)
    ->performRequest();

Twitter API Library further processed the operations by validating the URL pattern and performing the request.

Following case that depicts the exact scenario.

For example I have 2 Twitter Accounts using same keyword to search data.

If I fetch data with keyword1 & keyword2, data will be pulled successfully for keyword1 and then stops with error code. Still same data is being pulled for keyword2 with no errors.

If the issue is from coding side or requesting format, then the other keyword is not supposed to pull data as the same code, same format and same Rest API library is being used.

Hope this makes sense.


#14

When you are defining a request with keyword1 and keyword2, are you doing it like this:

$getfields = array( ‘keyword1’ => ‘value1’, ‘keyword2’ => ‘value2’);

Or like this:

$getfields = ‘keyword1=value1&keyword2=value2’;

?

Or something different?


#15

I have used the 2nd pattern when calling.

$getfields = ‘keyword1=value1&keyword2=value2’;

Which is mentioned in the Official Twitter API Library for PHP.

Following is the link.

This library converts normal params format to array format as mentioned in First pattern.


#16

That seems right. Can you log what content-type the TwitterAPIExchange library is choosing when it fails, as well as when it succeeds? In other words, can you log the request headers? I’m wondering if it’s choosing multipart/form when it fails.


#17

No, till now I was logging response only.
Now trying to log the headers also. But during these days I couldn’t get that issue to find the scenario.
As per my observation, in Library(mentioned above), headers set only for OAuth, not for content-type.


#18

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.