How to authenticate a user (User Context) - Getting error: Could not authenticate you



I’ve been trying to Implement Sign in with Twitter, but getting “error:Could not authenticate you.” If anyone can tell “why is that”.

Whatt i’m getting:

[http_code] => 401

[request_header] => POST /oauth/request_token HTTP/1.1
User-Agent: terminal
Accept: */*
Authorization: OAuth oauth_callback="", oauth_consumer_key="dfhaifaBJKJ*****KJGJKGK", oauth_nonce="0a2297e0f1db846dbce0b814b08c181c", oauth_signature="kjhfk*****%253L", 
oauth_signature_method="HMAC-SHA1", oauth_timestamp="1491821558", oauth_version="1.0"Content-Type: application/x-www-form-urlencoded
    Expect: 100-continue
    [errors] => Array
            [0] => Array
                    [code] => 32
                    [message] => Could not authenticate you.



Allow this application to be used to Sign in with Twitter is also checked.

Any help would be appreciated :slight_smile:


Are you able to share any code snippet? What language are you using?


I’m using php and below is the code i;m using:

    $url = "";
    $method = "POST";

    $oauth_callback = '';
    $oauth_consumer_key = 'consumer_key';
    $oauth_consumer_key_secret = 'consumer_secret';
    $oauth_nonce = time();
    $oauth_signature_method = 'HMAC-SHA1';
    $oauth_timestamp = time();
    $oauth_version = '1.0';

    $params = array(
        'oauth_callback'            =>  rawurlencode($oauth_callback),
        'oauth_consumer_key'        =>  rawurlencode($oauth_consumer_key),
        'oauth_nonce'               =>  rawurlencode($oauth_nonce),
        'oauth_signature_method'    =>  rawurlencode($oauth_signature_method),
        'oauth_timestamp'           =>  rawurlencode($oauth_timestamp),
        'oauth_version'             =>  rawurlencode($oauth_version)


    $parameter_string = http_build_query($params);

    $base_string = $method . '&';
    $base_string .= rawurlencode($url) . '&';
    $base_string .= $parameter_string;

    $oauth_signing_key = rawurlencode($oauth_consumer_key_secret) . '&';
    $oauth_signature = base64_encode(hash_hmac('sha1', $base_string, $oauth_signing_key, true));

    $params['oauth_signature'] = rawurlencode($oauth_signature);

	$post = '';
        foreach($params as $key=>$value){
            $post .= $key.'='.'"'.$value.'", ';

    $post = rtrim($post, ", ");

    $headers = array( 
        "POST /oauth/request_token HTTP/1.1",
        "User-Agent: ",
        "Accept: */*",          
        "Authorization: OAuth ". $post

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
		curl_setopt($ch, CURLOPT_CAINFO, __DIR__.'/cacert.pem');
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $retrievedhtml = curl_exec ($ch);
	echo '<pre>';


Hope it helps!


I would recommend using a library for the OAuth signatures. It’s very difficult to get correct. TwitterOAuth is my (biased) preferred on but there are a number of others as well.


Even using TwitteroAuth library, i’m getting that same error:

$r = $connection->oauth(‘oauth/request_token’, array(“oauth_callback” =>MY_CALLBACK_CONSTANT));

error code: 32 and message: could not authenticate you.
on TwitterOAuth.php line number 138

describing further, i’ve been following twitter rest api docs and was successful in accomplishing (Application context functionality [from scratch without any library] )

and now i’m doing same as docs suggesting but i don’t understand why this vague error keeps poping out for (User context), i don’t even know “what this error is??” or “What this error should mean??” or “What i’m doing wrong??” or “Is there a setting i forgot to look after??” :frowning:

For Application-only authentication i used this: and was successful.

For Implementing Sign in with Twitter i used this: :frowning:

and at last what do you mean when you say:

It’s very difficult to get correct


An authentication error for oauth/request_token with the TwitterOAuth library means you are probably not using the correct consumer key/consumer secret.

The error “Could not authenticate you.” means that the credentials on that request are invalid. They could be invalid for a number of reasons. The OAuth 1 signature was generated incorrectly (very common with custom built code that doesn’t use a library), the user has revoked access from the app, or credentials were not on the request but are required.

Application auth uses OAuth 2 where you basically get a token from Twitter and send that token with every request as the credentials. It’s easy but relies on HTTPS to keep it secure. This can only be used to get public data and requests are not attributed to any user.

User Context authentication uses OAuth 1 which is a very different ballgame and requires a number of values be sorted, encoded, and signed in a very specific method or when the request gets to Twitter their system won’t be able to validate the credentials return a “Could not authenticate you” error.


if the above code is correct and even after using twitterOAuth library i’m getting the same error, Is it possible that it’s twitter specific problem.

An authentication error for oauth/request_token with the TwitterOAuth library means you are probably not using the correct consumer key/consumer secret.

I also regenerated the (consumer key & secret) and ( access token & secret)

and (consumer key & secret) got changed but ( access token & secret) didn’t

Is that right??

And I’m Really Kinda Stuck In There, If You Could Help Me Out. Please!


For a oauth/request_token there should be no access token and secret yet. That API endpoint is the first step in generate an authorized access token and secret for one of your sites users. As documented this should look similar to the following code for the initial step.

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
$request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK));


Just wanted to say thanks it worked. :slight_smile: