1

我正在尝试使用 QOAuth 对 twitter 的 API 进行身份验证。

我目前的代码是:

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();
}`

并给我

temporary token "cBhxmmdkYgmghyy02kmfc0VSIuykRCNoQRh2h1r3Yg" "oYF8b2lzPSgDTRku8X4BjjnoVw5dAXXZBXc2R9P8Jk" 
ERROR 401

使用 QOAuth::POST 而不是 QOAuth::GET 我有相同的结果

如何使用 QOAuth 获取访问令牌?

4

1 回答 1

2

当我设法解决自己的问题时,我在这里发布了解决方案:

事实是,为了获得对https://api.twitter.com/oauth/access_token的访问权限,您需要一个密码,用户可以在https://api.twitter.com/oauth/authenticate获得该密码

Hewever 如果您在https://api.twitter.com/oauth/request_token请求临时令牌时设置 oauth_callback=oob,则只能获得此 pin

我最终得到了以下代码:

    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();
    }
于 2012-07-11T15:50:13.557 回答