1

I am trying to get authentification to twitter's API using QOAuth.

my code currently is :

oauthInterface->setConsumerKey(CONSUMER_KEY);
oauthInterface->setConsumerSecret(CONSUMER_SECRET_KEY);
oauthInterface->setRequestTimeout(10000);

QOAuth::ParamMap reply = oauthInterface->requestToken("https://api.twitter.com/oauth/request_token", QOAuth::GET, QOAuth::HMAC_SHA1);

if(oauthInterface->error() == QOAuth::NoError)
{
    token = reply.value(QOAuth::tokenParameterName());
    tokenSecret = reply.value(QOAuth::tokenSecretParameterName());

    qDebug() << "temporary token" << token << tokenSecret;
}

reply = oauthInterface->accessToken("https://api.twitter.com/oauth/access_token",QOAuth::GET, token, tokenSecret, QOAuth::HMAC_SHA1);

if(oauthInterface->error() == QOAuth::NoError)
{
    qDebug() << "final token" << reply.value("screen_name") << reply.value(QOAuth::tokenParameterName()) << reply.value(QOAuth::tokenSecretParameterName());
}
else
{
    qDebug() << "ERROR" << oauthInterface->error();
}`

And gives me

temporary token "cBhxmmdkYgmghyy02kmfc0VSIuykRCNoQRh2h1r3Yg" "oYF8b2lzPSgDTRku8X4BjjnoVw5dAXXZBXc2R9P8Jk" 
ERROR 401

Using QOAuth::POST instead of QOAuth::GET I have the same result

How am I to get access token's using QOAuth ?

Amxx
  • 3,020
  • 2
  • 24
  • 45

1 Answers1

2

As I managed to solve my own probleme I post here the solution :

The fact is, for beeing granted access to https://api.twitter.com/oauth/access_token you need a pin which can be obtained by the user at https://api.twitter.com/oauth/authenticate

Hewever you can only get this pin if you set oauth_callback=oob when asking for temporary tokens at https://api.twitter.com/oauth/request_token

I finaly ended up with the following code :

    oauthInterface->setConsumerKey(CONSUMER_KEY);
    oauthInterface->setConsumerSecret(CONSUMER_SECRET_KEY);
    oauthInterface->setRequestTimeout(10000);

    QOAuth::ParamMap args;
        args.insert("oauth_callback", "oob");
    QOAuth::ParamMap reply = oauthInterface->requestToken("https://api.twitter.com/oauth/request_token", QOAuth::POST, QOAuth::HMAC_SHA1, args);

    if(oauthInterface->error() == QOAuth::NoError)
    {
        token = reply.value(QOAuth::tokenParameterName());
        tokenSecret = reply.value(QOAuth::tokenSecretParameterName());

        qDebug() << "temporary token" << token << tokenSecret;
    }

    QString url = "https://api.twitter.com/oauth/authenticate";
    url.append("?");
    url.append(QOAuth::tokenParameterName() + "=" + token);
    QDesktopServices::openUrl(QUrl(url));


    QOAuth::ParamMap args2;
    QString pin = QInputDialog::getText(this, "Pin", "Enter pin");
    args2.insert("oauth_verifier", pin.toAscii()); //pin.toAscii());
    reply = oauthInterface->accessToken("https://api.twitter.com/oauth/access_token", QOAuth::GET, token, tokenSecret, QOAuth::HMAC_SHA1, args2);


    if(oauthInterface->error() == QOAuth::NoError)
    {
        qDebug() << "final token" << reply.value("screen_name") << reply.value(QOAuth::tokenParameterName()) << reply.value(QOAuth::tokenSecretParameterName());
    }
    else
    {
        qDebug() << "ERROR" << oauthInterface->error();
    }
Amxx
  • 3,020
  • 2
  • 24
  • 45
  • 1
    For the intermediate request, it may be https://api.twitter.com/oauth/authorize instead of https://api.twitter.com/oauth/authenticate . It depends on your application The right URL is written in the application settings on Twitter Developer. – air-dex Jul 12 '12 at 00:01
  • Both are accepted, they just do different things. People should refer to twitter's api. If one user may open multiple accounts, use authorize – Amxx Jul 12 '12 at 10:26