Fetch Dropbox account information after authorisation.
This commit is contained in:
parent
d3bd536be0
commit
4d1b6fc25d
@ -14,6 +14,7 @@ namespace {
|
||||
static const char* kAppKey = "qh6ca27eclt9p2k";
|
||||
static const char* kAppSecret = "pg7y68h5efap8r6";
|
||||
|
||||
// OAuth 1.0 endpoints
|
||||
static const char* kRequestTokenEndpoint =
|
||||
"https://api.dropbox.com/1/oauth/request_token";
|
||||
static const char* kAuthoriseEndpoint =
|
||||
@ -21,6 +22,10 @@ static const char* kAuthoriseEndpoint =
|
||||
static const char* kAccessTokenEndpoint =
|
||||
"https://api.dropbox.com/1/oauth/access_token";
|
||||
|
||||
// Dropbox API endpoints
|
||||
static const char* kAccountInfoEndpoint =
|
||||
"https://api.dropbox.com/1/account/info";
|
||||
|
||||
} // namespace
|
||||
|
||||
DropboxAuthenticator::DropboxAuthenticator(QObject* parent)
|
||||
@ -54,17 +59,28 @@ void DropboxAuthenticator::StartAuthorisation(const QString& email) {
|
||||
this, SLOT(RequestTokenFinished(QNetworkReply*)), reply);
|
||||
}
|
||||
|
||||
void DropboxAuthenticator::RequestTokenFinished(QNetworkReply* reply) {
|
||||
QString result = reply->readAll();
|
||||
QStringList components = result.split("&");
|
||||
QMap<QString, QString> params;
|
||||
namespace {
|
||||
|
||||
// Parse a string like a=b&c=d into a map.
|
||||
QMap<QString, QString> ParseParamList(const QString& params) {
|
||||
QMap<QString, QString> ret;
|
||||
QStringList components = params.split("&");
|
||||
foreach (const QString& component, components) {
|
||||
QStringList pairs = component.split("=");
|
||||
if (pairs.size() != 2) {
|
||||
continue;
|
||||
}
|
||||
params[pairs[0]] = pairs[1];
|
||||
ret[pairs[0]] = pairs[1];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void DropboxAuthenticator::RequestTokenFinished(QNetworkReply* reply) {
|
||||
reply->deleteLater();
|
||||
QString result = reply->readAll();
|
||||
QMap<QString, QString> params = ParseParamList(result);
|
||||
token_ = params["oauth_token"];
|
||||
secret_ = params["oauth_token_secret"];
|
||||
Authorise();
|
||||
@ -132,5 +148,46 @@ void DropboxAuthenticator::RequestAccessToken() {
|
||||
}
|
||||
|
||||
void DropboxAuthenticator::RequestAccessTokenFinished(QNetworkReply* reply) {
|
||||
reply->deleteLater();
|
||||
QString result = QString::fromAscii(reply->readAll());
|
||||
qLog(Debug) << result;
|
||||
QMap<QString, QString> params = ParseParamList(result);
|
||||
access_token_ = params["oauth_token"];
|
||||
access_token_secret_ = params["oauth_token_secret"];
|
||||
qLog(Debug) << Q_FUNC_INFO << access_token_ << access_token_secret_;
|
||||
RequestAccountInformation();
|
||||
}
|
||||
|
||||
QByteArray DropboxAuthenticator::GenerateAuthorisationHeader() {
|
||||
typedef QPair<QString, QString> Param;
|
||||
QByteArray signature = QUrl::toPercentEncoding(
|
||||
QString("%1&%2").arg(kAppSecret, access_token_secret_));
|
||||
QList<Param> params;
|
||||
params << Param("oauth_consumer_key", kAppKey)
|
||||
<< Param("oauth_signature_method", "PLAINTEXT")
|
||||
<< Param("oauth_timestamp", QString::number(time(NULL)))
|
||||
<< Param("oauth_nonce", QString::number(qrand()))
|
||||
<< Param("oauth_signature", signature)
|
||||
<< Param("oauth_token", access_token_);
|
||||
QStringList encoded_params;
|
||||
foreach (const Param& p, params) {
|
||||
encoded_params << QString("%1=\"%2\"").arg(p.first, p.second);
|
||||
}
|
||||
QString authorisation_header = QString("OAuth ") + encoded_params.join(", ");
|
||||
return authorisation_header.toUtf8();
|
||||
}
|
||||
|
||||
void DropboxAuthenticator::RequestAccountInformation() {
|
||||
QUrl url(kAccountInfoEndpoint);
|
||||
QNetworkRequest request(url);
|
||||
request.setRawHeader("Authorization", GenerateAuthorisationHeader());
|
||||
qLog(Debug) << Q_FUNC_INFO << url << request.rawHeader("Authorization");
|
||||
QNetworkReply* reply = network_->get(request);
|
||||
NewClosure(reply, SIGNAL(finished()),
|
||||
this, SLOT(RequestAccountInformationFinished(QNetworkReply*)), reply);
|
||||
}
|
||||
|
||||
void DropboxAuthenticator::RequestAccountInformationFinished(QNetworkReply* reply) {
|
||||
reply->deleteLater();
|
||||
qLog(Debug) << reply->readAll();
|
||||
}
|
||||
|
@ -18,18 +18,28 @@ class DropboxAuthenticator : public QObject {
|
||||
void RedirectArrived(QTcpSocket* socket, QByteArray buffer);
|
||||
void NewConnection();
|
||||
void RequestAccessTokenFinished(QNetworkReply* reply);
|
||||
void RequestAccountInformationFinished(QNetworkReply* reply);
|
||||
|
||||
private:
|
||||
void Authorise();
|
||||
void RequestAccessToken();
|
||||
QByteArray GenerateAuthorisationHeader();
|
||||
void RequestAccountInformation();
|
||||
|
||||
private:
|
||||
NetworkAccessManager* network_;
|
||||
QTcpServer server_;
|
||||
|
||||
// Temporary access token used for first authentication flow.
|
||||
QString token_;
|
||||
QString secret_;
|
||||
|
||||
// User's Dropbox uid.
|
||||
QString uid_;
|
||||
|
||||
// Permanent OAuth access tokens.
|
||||
QString access_token_;
|
||||
QString access_token_secret_;
|
||||
};
|
||||
|
||||
#endif // DROPBOXAUTHENTICATOR_H
|
||||
|
Loading…
x
Reference in New Issue
Block a user