File indexing completed on 2025-01-19 03:55:38

0001 #include <QNetworkAccessManager>
0002 #include <QNetworkRequest>
0003 #include <QNetworkReply>
0004 #include <QDesktopServices>
0005 #include <QMetaEnum>
0006 #include <QDebug>
0007 
0008 #include "ytdemo.h"
0009 #include "o0globals.h"
0010 #include "o0settingsstore.h"
0011 #include "o2requestor.h"
0012 
0013 const char GOOGLE_APP_KEY[]    = "YOUR_YOUTUBE_APP_KEY";
0014 const char GOOGLE_APP_SECRET[] = "YOUR_YOUTUBE_APP_SECRET";
0015 const char YOUTUBE_SCOPE[] = "https://www.googleapis.com/auth/youtube.upload https://www.googleapis.com/auth/youtube";
0016 const char YOUTUBE_CHANNELS_LIST_URL[] = "https://www.googleapis.com/youtube/v3/channels?part=status&mine=true";
0017 
0018 const int localPort = 8888;
0019 
0020 #define QENUM_NAME(o,e,v) (o::staticMetaObject.enumerator(o::staticMetaObject.indexOfEnumerator(#e)).valueToKey((v)))
0021 #define GRANTFLOW_STR(v) QString(QENUM_NAME(O2, GrantFlow, v))
0022 
0023 YTDemo::YTDemo(QObject *parent) :
0024     QObject(parent), requestId_(0) {
0025     o2Google_ = new O2Google(this);
0026 
0027     o2Google_->setClientId(GOOGLE_APP_KEY);
0028     o2Google_->setClientSecret(GOOGLE_APP_SECRET);
0029     o2Google_->setLocalPort(localPort);
0030     o2Google_->setScope(YOUTUBE_SCOPE);
0031 
0032     // Create a store object for writing the received tokens
0033     O0SettingsStore *store = new O0SettingsStore(O2_ENCRYPTION_KEY);
0034     store->setGroupKey("google");
0035     o2Google_->setStore(store);
0036 
0037     connect(o2Google_, SIGNAL(linkedChanged()), this, SLOT(onLinkedChanged()));
0038     connect(o2Google_, SIGNAL(linkingFailed()), this, SIGNAL(linkingFailed()));
0039     connect(o2Google_, SIGNAL(linkingSucceeded()), this, SLOT(onLinkingSucceeded()));
0040     connect(o2Google_, SIGNAL(openBrowser(QUrl)), this, SLOT(onOpenBrowser(QUrl)));
0041     connect(o2Google_, SIGNAL(closeBrowser()), this, SLOT(onCloseBrowser()));
0042 }
0043 
0044 void YTDemo::doOAuth(O2::GrantFlow grantFlowType) {
0045     qDebug() << "Starting OAuth 2 with grant flow type" << GRANTFLOW_STR(grantFlowType) << "...";
0046     o2Google_->setGrantFlow(grantFlowType);
0047     o2Google_->unlink();
0048     o2Google_->link();
0049 }
0050 
0051 void YTDemo::getUserChannelInfo() {
0052     if (!o2Google_->linked()) {
0053         qWarning() << "ERROR: Application is not linked!";
0054         emit linkingFailed();
0055         return;
0056     }
0057 
0058     QString channelsListUrl = QString(YOUTUBE_CHANNELS_LIST_URL);
0059     QNetworkRequest request = QNetworkRequest(QUrl(channelsListUrl));
0060     QNetworkAccessManager *mgr = new QNetworkAccessManager(this);
0061     O2Requestor *requestor = new O2Requestor(mgr, o2Google_, this);
0062     requestId_ = requestor->get(request);
0063     connect(requestor, SIGNAL(finished(int, QNetworkReply::NetworkError, QByteArray)),
0064         this, SLOT(onFinished(int, QNetworkReply::NetworkError, QByteArray))
0065     );
0066     qDebug() << "Getting user channel info... Please wait.";
0067 }
0068 
0069 void YTDemo::onOpenBrowser(const QUrl &url) {
0070     QDesktopServices::openUrl(url);
0071 }
0072 
0073 void YTDemo::onCloseBrowser() {
0074 }
0075 
0076 void YTDemo::onLinkedChanged() {
0077     qDebug() << "Link changed!";
0078 }
0079 
0080 void YTDemo::onLinkingSucceeded() {
0081     O2Google *o2t = qobject_cast<O2Google *>(sender());
0082     if (!o2t->linked()) {
0083         return;
0084     }
0085     QVariantMap extraTokens = o2t->extraTokens();
0086     if (!extraTokens.isEmpty()) {
0087         emit extraTokensReady(extraTokens);
0088         qDebug() << "Extra tokens in response:";
0089         foreach (QString key, extraTokens.keys()) {
0090             qDebug() << "\t" << key << ":" << (extraTokens.value(key).toString().left(3) + "...");
0091         }
0092     }
0093     emit linkingSucceeded();
0094 }
0095 
0096 void YTDemo::onFinished(int requestId, QNetworkReply::NetworkError error, QByteArray replyData) {
0097     if (requestId != requestId_)
0098         return;
0099 
0100     if (error != QNetworkReply::NoError) {
0101         qWarning() << "Reply error:" << error;
0102         emit channelInfoFailed();
0103         return;
0104     }
0105 
0106     QString reply(replyData);
0107     bool errorFound = reply.contains("error");
0108     if (errorFound) {
0109         qDebug() << "Request failed";
0110         emit channelInfoFailed();
0111         return;
0112     }
0113 
0114     qInfo() << "Channel info: " << reply;
0115     emit channelInfoReceived();
0116 }
0117 
0118 #include "moc_ytdemo.cpp"