Как известно У меня есть один файл заголовка (oauth2_google.h): < /p>
Код: Выделить всё
class GoogleSSO : public QObject
{
Q_OBJECT
Q_PROPERTY(QByteArray loginData READ loginData WRITE setLoginData NOTIFY loginDataChanged)
public:
explicit GoogleSSO(QObject *parent = nullptr);
virtual ~GoogleSSO(); // dont even know what is this, maybe someone can explain?
// triggered by QML (only ready)
QByteArray loginData() const { return m_loginData; }
// triggered by QML: loginData = ""; // or by C++: setLoginData(string)
void setLoginData(const QByteArray &newData) { ... }
public slots:
Q_INVOKABLE void authenticate(); // Invoked externally to initiate
private:
QOAuth2AuthorizationCodeFlow *google;
QByteArray m_loginData;
QString generateRandomString() { ... }
QString replyMessage = "LOGIN FINISHED OK!";
signals:
void loginDataChanged(/*QByteArray*/);
};
< /code>
и исходный файл (oauth2_google.cpp): < /p>
#include "oauth2_google.h"
GoogleSSO::GoogleSSO(QObject *parent) : QObject(parent)
{
this->google = new QOAuth2AuthorizationCodeFlow(this);
this->google->setScope("https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile");
this->google->setClientIdentifier("........");
....
this->google->setClientIdentifierSharedKey("..........");
QObject::connect(this->google, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser, &QDesktopServices::openUrl);
// set the webpage content to show the user
QOAuthHttpServerReplyHandler *replyHandler = new QOAuthHttpServerReplyHandler(port, this);
replyHandler->setCallbackText(replyMessage); // this is from header file
this->google->setReplyHandler(replyHandler);
// HANDLER WHEN LOGGIN FAILS
QObject::connect(this->google, &QOAuth2AuthorizationCodeFlow::authorizationCallbackReceived, this, [&](const QVariantMap &data) {
QString error = data.value("error").toString();
if (!error.isEmpty()) {
....
setLoginData(....);
// QUESTION #2... in case of login failed, i want to change the replyMessage in header file... how do I do it from here?
QString replyMessage = "LOGIN FAILED";
}
});
// HANDLER WHEN LOGGIN SUCCEED
QObject::connect(this->google, &QOAuth2AuthorizationCodeFlow::granted, this, [this](){
// send the token to receive needed info
QNetworkRequest req; req.setUrl(QUrl("https://people.googleapis.com/v1/people/me?personFields=emailAddresses,photos,names&access_token=" + this->google->token()));
QNetworkAccessManager *manager = new QNetworkAccessManager();
QNetworkReply *rep = manager->get(req);
QObject::connect(manager, &QNetworkAccessManager::finished, this, [this](QNetworkReply *reply) {
QByteArray serverReply = reply->readAll();
...
setLoginData(....);
reply->deleteLater();
});
});
}
GoogleSSO::~GoogleSSO() { delete this->google; }
void GoogleSSO::authenticate() { this->google->grant(); }
< /code>
На данный момент это работает отлично ...
Однако сейчас проблема в том, что мне нужно ввести также вход в Facebook Oauth в приложении, поэтому я хочу иметь отдельный источник Файл (потому что я буду иметь дело с каким -то сутфом там по -разному), но как можно больше я хотел бы сохранить один общий файл заголовка для обоих исходных файлов ... < /p>
Итак, давайте рассмотрим, что у меня есть Второй исходный файл (oauth2_firebase.cpp): < /p>
FacebookSSO::FacebookSSO(QObject *parent) : QObject(parent)
{
this->facebook = new QOAuth2AuthorizationCodeFlow(this);
this->facebook->setScope("public_profile,email");
this->facebook->setClientIdentifier("........");
....
this->facebook->setClientIdentifierSharedKey("..........");
QObject::connect(this->facebook, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser, &QDesktopServices::openUrl);
// set the webpage content to show the user
QOAuthHttpServerReplyHandler *replyHandler = new QOAuthHttpServerReplyHandler(port, this);
replyHandler->setCallbackText(replyMessage); // this is from header file
this->facebook->setReplyHandler(replyHandler);
// HANDLER WHEN LOGGIN FAILS
QObject::connect(this->facebook, &QOAuth2AuthorizationCodeFlow::authorizationCallbackReceived, this, [&](const QVariantMap &data) {
QString error = data.value("error").toString();
if (!error.isEmpty()) {
....
setLoginData(....);
// QUESTION #2... in case of login failed, i want to change the replyMessage in header file... how do I do it from here? - same as in first source
QString replyMessage = "LOGIN FAILED";
}
});
// HANDLER WHEN LOGGIN SUCCEED
QObject::connect(this->facebook, &QOAuth2AuthorizationCodeFlow::granted, this, [this](){
// send the token to receive needed info
QNetworkRequest req; req.setUrl(QUrl("https://graph.facebook.com/me?fields=id,name,email,picture.width(320).height(320)&access_token=" + this->facebook->token()));
QNetworkAccessManager *manager = new QNetworkAccessManager();
QNetworkReply *rep = manager->get(req);
QObject::connect(manager, &QNetworkAccessManager::finished, this, [this](QNetworkReply *reply) {
QByteArray serverReply = reply->readAll();
...
setLoginData(....);
reply->deleteLater();
});
});
}
FacebookSSO::~FacebookSSO() { delete this->facebook; }
void FacebookSSO::authenticate() { this->facebook->grant(); }
- of course considering renaming authenticate() functions for each, so one will be googleAuthenticate() while second facebookAuthenticate()
- both functions should change one same variable loginData from header file
i think better here would be to declare and set two string sin header ("replyMessagePASS" and "replyMessageFAIL") and Каким-то образом переключитесь между ними, позвонив: ReplyHandler-> setCallbackText (ReplyMessagePass) < /code>
или
Код: Выделить всё
replyHandler->setCallbackText(replyMessageFAIL)Подробнее здесь: https://stackoverflow.com/questions/794 ... urce-files