Skip to content

Instantly share code, notes, and snippets.

@rajatkhanduja
Created May 11, 2014 13:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rajatkhanduja/edbb7f8d162912b38157 to your computer and use it in GitHub Desktop.
Save rajatkhanduja/edbb7f8d162912b38157 to your computer and use it in GitHub Desktop.
OAuth for Evernote using O2 (https://github.com/pipacs/o2) and Qt
#ifndef EVERNOTE_AUTHENTICATOR_H
#define EVERNOTE_AUTHENTICATOR_H
#include "o2/o1.h"
#include <QString>
class EvernoteAuthenticator : public O1{
public:
EvernoteAuthenticator(const QString& host, const QString& customerKey, const QString& customerSecret, QObject *parent = 0){
this->setRequestTokenUrl(QUrl(host + "/oauth"));
this->setAuthorizeUrl(QUrl(host + "/OAuth.action"));
this->setAccessTokenUrl(QUrl(host + "/oauth"));
this->setClientId(customerKey);
this->setClientSecret(customerSecret);
}
};
#endif // EVERNOTE_AUTHENTICATOR_H
#include "EvernoteClient.h"
void EvernoteClient::link()
{
view = new QWebView();
connect(evernoteAuthenticator, SIGNAL(openBrowser(QUrl)), this, SLOT(onOpenBrowser(QUrl)));
connect(evernoteAuthenticator, SIGNAL(closeBrowser()), this, SLOT(onCloseBrowser()));
connect(evernoteAuthenticator, SIGNAL(linkingSucceeded()), this, SLOT(onLinkingSucceeded()));
evernoteAuthenticator->link();
}
void EvernoteClient::unLink(){
evernoteAuthenticator->unlink();
}
EvernoteClient::EvernoteClient(const QString& customerKey, const QString& customerSecret, bool sandbox, QObject *parent) : QObject(parent){
this->sandbox = sandbox;
evernoteAuthenticator = new EvernoteAuthenticator(getHost(), customerKey, customerSecret);
}
QString EvernoteClient::getHost() const{
if(sandbox){
return "https://sandbox.evernote.com";
}
else{
return "https://www.evernote.com";
}
}
void EvernoteClient::onLinkingSucceeded(){
delete view;
if (!evernoteAuthenticator->linked()) return;
emit linkingSucceeded();
}
void EvernoteClient::onOpenBrowser(QUrl url){
std::cerr << "Opening browser " << url.toString().toStdString() << std::endl;
view->load(url);
view->show();
}
void EvernoteClient::onCloseBrowser(){
view->close();
}
#ifndef EVERNOTECLIENT_H
#define EVERNOTECLIENT_H
/**
* @brief Class to initialize connection with Evernote. This class
* is responsible for OAuth
*/
#include <EvernoteAuthenticator.h>
#include <QWebView>
#include <QObject>
class EvernoteClient : public QObject
{
Q_OBJECT
public:
explicit EvernoteClient(const QString& customerKey, const QString& customerSecret, bool sandbox = true, QObject *parent = 0);
~EvernoteClient(){}
void link();
void unLink();
Q_SIGNALS:
void linkingSucceeded();
public Q_SLOTS:
void onLinkingSucceeded();
void onOpenBrowser(QUrl url);
void onCloseBrowser();
protected:
QString getHost() const;
private:
bool sandbox;
EvernoteAuthenticator *evernoteAuthenticator;
QWebView *view;
};
#endif // EVERNOTECLIENT_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment