summaryrefslogtreecommitdiffstats
path: root/tests/manual/examples/twittertimeline/twitter.cpp
blob: 54d99c5cde47f6911ff20da10d61ae8a1f8c5c9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include "twitter.h"

#include <QtGui>
#include <QtCore>
#include <QtNetwork>

Twitter::Twitter(QObject *parent) :
    Twitter(QString(), std::make_pair(QString(), QString()), parent)
{}

Twitter::Twitter(const std::pair<QString, QString> &clientCredentials, QObject *parent) :
    Twitter(QString(), clientCredentials, parent)
{}

Twitter::Twitter(const QString &screenName,
                 const std::pair<QString, QString> &clientCredentials,
                 QObject *parent) :
    QOAuth1(clientCredentials.first, clientCredentials.second, nullptr, parent)
{
    replyHandler = new QOAuthHttpServerReplyHandler(1337, this);
    replyHandler->setCallbackPath("callback");
    setReplyHandler(replyHandler);
    setTemporaryCredentialsUrl(QUrl("https://api.twitter.com/oauth/request_token"));
    setAuthorizationUrl(QUrl("https://api.twitter.com/oauth/authenticate"));
    setTokenCredentialsUrl(QUrl("https://api.twitter.com/oauth/access_token"));

    connect(this, &QAbstractOAuth::authorizeWithBrowser, [=](QUrl url) {
        QUrlQuery query(url);

        // Forces the user to enter their credentials to authorize the correct
        // user account
        query.addQueryItem("force_login", "true");

        if (!screenName.isEmpty())
            query.addQueryItem("screen_name", screenName);
        url.setQuery(query);
        QDesktopServices::openUrl(url);
    });

    connect(this, &QOAuth1::granted, this, &Twitter::authenticated);

    if (!clientCredentials.first.isEmpty() && !clientCredentials.second.isEmpty())
        grant();
}