1

Suddenly the code to get Twitter login URL stopped working.

Error returned by generated URL to get outh_token is:

<error code="32">Could not authenticate you.</error>

URL generated to get outh_token is:

https://api.twitter.com/oauth/request_token?oauth_callback=http://project.local.com/dashboard/redirect-from-twitter&oauth_consumer_key=if5hivS82GJURRJMp60CjXQVd&oauth_nonce=1426244533&oauth_signature=Ag16Q1uwiiAJsLu5Zbl3oy1hil4%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1426244533&oauth_version=1.0

It seems as if something is wrong with signature.

Following is my code to generate signature:

public function buildsignature($method, $params, $url, $access_token_secret = null)
{
    $keys = $this->_urlencode_rfc3986(array_keys($params));
    $values = $this->_urlencode_rfc3986(array_values($params));
    $params = array_combine($keys, $values);
    uksort($params, 'strcmp');

    // convert params to string
    foreach ($params as $k => $v) {
        $pairs[] = $this->_urlencode_rfc3986($k) . '=' . $this->_urlencode_rfc3986($v);
    }
    $concatenatedParams = implode('&', $pairs);

    // form base string (first key)
    $baseString = $method . "&" . $this->_urlencode_rfc3986($url) . "&" . $this->_urlencode_rfc3986($concatenatedParams);
    // form secret (second key)

    if ($access_token_secret) {

        $key_parts = array(
            self::CLIENT_SECRET,
            $access_token_secret
        );
        $key_parts = $this->_urlencode_rfc3986($key_parts);
        $secret = implode('&', $key_parts);

            //$secret = rawurlencode(self::CLIENT_SECRET) . '&' . rawurlencode($access_token_secret);
        } else {
             $secret = $this->_urlencode_rfc3986(self::CLIENT_SECRET)."&";
        }

        return base64_encode(hash_hmac('sha1', $baseString, $secret, TRUE));
}

I tried answers from:

http://stackoverflow.com/questions/9861756/signin-with-twitter-stopped-working-suddenly

But I'm testing it on my local system, so I hope that there isn't due to any time gap. It's not creating the URL to be used for login purpose.

  • What is the question? – Jay Blanchard Mar 13 '15 at 11:48
  • I want to make it work again, I want to get twitter login url. It was working but not now. – Vipul Mar 13 '15 at 11:53
  • What changed? Something must have changed. – Jay Blanchard Mar 13 '15 at 11:55
  • Yeah it could be but I don't understand whats wrong as it was working few days back. – Vipul Mar 13 '15 at 12:01
  • You will not know until you know what has changed. Code doesn't go from working to not working without changes. – Jay Blanchard Mar 13 '15 at 12:02
1

I modified my code and replaced:

$pairs[] = $this->_urlencode_rfc3986($k) . '=' . $this->_urlencode_rfc3986($v);

with

$pairs[] = $k . '=' . $v;

where $this->_urlencode_rfc3986() is

public function _urlencode_rfc3986($input)
{
        if (is_array($input)) {
            return array_map($this->_urlencode_rfc3986, $input);
        }
        else if (is_scalar($input)) {
            return str_replace('+',' ',str_replace('%7E', '~', rawurlencode($input)));
        }
        else{
            return '';
        }
}

Now that's crazy because that previous code was working from last few months, it stopped suddenly. But the question is how could someone find such issues.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.