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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
kQOAuth is a OAuth 1.0 library written for Qt in C++. The goals for the
library have been to provide easy integration to existing Qt applications
utilizing Qt signals describing the OAuth process, and to provide a
convenient approach to OAuth authentication.
kQOAuth has support for retrieving the user authorization from the service
provider's website. kQOAuth will open the user's web browser to the
authorization page, give a local URL as the callback URL and setup a HTTP
server on this address to listen for the reply from the service and then
process it.
BUILDING kQOAuth
===============================
- Make sure that you have Qt 4.7 or greater installed
For OS X and Linux:
- run "qmake"
- run "make" to build
- Run "sudo make install" to install
- To run unit tests you need to set platform specific variable to point
correct directory in order to make unit tests to find the library. Note
that it doesn't require kQOAuth to be installed in order to run unit tests.
* for Linux: export LD_LIBRARY_PATH=/path/to/kQOAuth/lib/dir
* for OS X: export DYLD_LIBRARY_PATH=/path/to/kQOAuth/lib/dir
For Windows with the Visual Studio or Windows SDK Compiler
- run "qmake CONFIG+=release"
- run "nmake" to build
- run "nmake install" to install
COMPONENTS
===============================
Below I will present the two fundamental components necessary when using
kQOAuth; KQOAuthManager and KQOAuthRequest.
KQOAuthManager
===============================
Executes OAuth requests (KQOAuthRequest), parses the replies and emits Qt
signals describing the process of the OAuth authentication.
KQOAuthManager will keep track of the state of the authorization process.
This means that the provided convenience methods can be used to achieve the
necessary steps in OAuth authentication without explicitly constructing
each request.
Methods
-------------------------------
* void executeRequest(KQOAuthRequest *request)
- request: The KQOAuthRequest to process.
Takes as parameter a pointer to a KQOAuthRequest describing the request
that will be executed. The call will return as soon as the HTTP
request has been sent.
When the service responded to this request the signal
requestReady(QByteArray) is emitted. This will contain the raw data
that the service sent back as a response to the request.
If an error in the request was detected, this method will set the
error code and emit the requestReady(QByteArray) signal with
an empty QByteArray. The error code can be retrieved by calling lastError().
* void executeAuthorizedRequest(KQOAuthRequest *request, int id)
- request: The KQOAuthRequest to process.
- id: An id that is unique to the request.
This method is useful for situations where the reponses from a server
have different processing requirements.
The arguments and behavior of this method is similar to executeRequest()
with the addition of an id that is mapped to the request. When the service
has responded, the signal authorizedRequestReady(QByteArray, int) is emitted
where the int is the mapped id.
* void setHandleUserAuthorization(bool set)
- set: A boolean value telling KQOAuthManager to process the user's
authorization to protected resources.
If KQOAuth is to handle user authorization this method must be called
with argument set as 'true' before a request for the temporary token has
been executed.
* void getUserAuthorization(QUrl authorizationEndpoint);
- authorizationEndpoint: The URL to the service provider's authorization
web page.
A convenience method to retrieve the user authorization to access the
protected resources. This method will open user's web browser to the
service providers authorization page given as argument and setup a
a local HTTP server to process the response.
This method must be called after temporaryTokenReceived(...) signal
is emitted.
When the user's authentication is processed, the signal
authorizationReceived(...) is emitted.
* void getUserAccessTokens(QUrl accessTokenEndpoint);
- accessTokenEndpoint: The URL to the service provider which is used for
retrieving the access token.
A convenience method to retrieve the access token, which is used for
accessing the protected resources.
This method must be called after the signal authorizationReceived(...)
is received.
When the access token request is processed, the signal accessTokenReceived(...)
is emitted. After this signal the protected resources can be accessed.
* void sendAuthorizedRequest(QUrl requestEndpoint,
const KQOAuthParameters &requestParameters);
- requestEndpoint: The URL to the service provider which can be used for
accessing protected resources.
- requestParameters: The parameters for this request which are service
specific. KQOAuthParameters is a typecasted
QMultiMap<QString, QString> for parameter name and value.
A convenience method for accessing the protected resources. The
parameters are given as KQOAuthParameters, which is a typecasted
QMultiMap<QString parameterName, QString parameterValue>. Please
consult your service provider for which parameters to use.
* bool hasTemporaryToken()
Returns true if the temporary token has been received. Otherwise returns
false
* bool isVerified()
Returns true if the user has successfully verified us to access the protected
resources. Otherwise returns false.
* bool isAuthorized()
Returns true if we have access tokens to access the protected resources.
Otherwise returns false.
* KQOAuthError lastError()
Returns the most recent error code in the authentication process.
Signals
-------------------------------
* void requestReady(QByteArray networkReply)
- networkReply: The response as sent from the service the each request.
This signal is emitted after each request has been processed. It will
return all the request response parameters in the QMultiMap, where
the first parameter is the name and the second parameter is the value.
* void authorizedRequestReady(QByteArray networkReply, int id);
- networkReply: The response as sent from the service the each request.
- id: The id that is mapped to the original request
This signal is emitted after each authorizedRequest has been processed.
* void receivedToken(QString oauth_token, QString oauth_token_secret)
- oauth_oken: Token value
- oauth_token_secret: Token secret value
This signal is emitted after a successful request for temporary tokens or
access tokens. The requested tokens are sent as parameters in this
signal.
* void temporaryTokenReceived(QString oauth_token, QString oauth_token_secret)
- oauth_token: Temporary token value
- oauth_token_secret: Temporary token secret value
This signal is emitted after a successful request for temporary tokens.
The tokens are sent as parameters to this signal.
* void authorizationReceived(QString oauth_token, QString oauth_verifier)
- oauth_token: The temporary token that was used when requesting access.
- oauth_verifier: The oauth_verifier token received as a reply to the
request. This value is necessary when requesting access token.
This signal is emitted after the user has concluded the authorization request
step in the service provider's website.
Note that setHandleUserAuthorization(true) must be called when requesting for
the temporary token.
* void accessTokenReceived(QString oauth_token, QString oauth_token_secret)
- oauth_token: The access token value.
- oauth_token_secret: The access token secret value.
This signal is emitted after a successful request for access tokens. The
requested tokens are sent as parameters in this signal. These tokens
are necessary when requesting for protected resources.
KQOAuthRequest
====================================
KQOAuthRequest is given to KQOAuthManager to execute the requested OAuth
request. It will also take care of parameter normalization, generating
the needed authorization header, and most importantly, generate a
OAuth signature.
Methods
------------------------------------
* void initRequest(KQOAuthRequest::RequestType type, const QUrl &requestEndpoint)
- type: Initializes this request to be of type 'KQOAuthRequest::RequestType'.
- requestEndpoint: The URL to the service provider that can handle the
requests of type 'KQOAuthRequest::RequestType'.
Initializes the request to be of a certain type that will be sent to
service provider's HTTP location. The request can be of type:
- TemporaryCredentials: Requesting the temporary credentials.
- AccessToken: Request the access tokens.
- AuthorizedRequest: When OAuth process is complete, we can access the
protected resources with this request.
* void setConsumerKey(const QString &consumerKey)
Set the consumer key to 'consumerKey'. This is given by the service provider.
* void setConsumerSecretKey(const QString &consumerSecretKey)
Set the consumer key secret to 'consumerSecretKey'. This is given by the
service provider.
* void setCallbackUrl(const QUrl &callbackUrl)
If the request type is 'KQOAuthRequest::TemporaryCredentials', then this
is the callback URL for the request. This is needed when asking user
authorization.
* void setTokenSecret(const QString &tokenSecret)
If the request type is 'KQOAuthRequest::AccessToken' then this is the
temporary token secret from the earlier request returned by the service
provider.
* void setToken(const QString &token)
If the request type is 'KQOAuthRequest::AccessToken' then this is the
temporary token from the earlier request returned by the service provider.
* void setSignatureMethod(KQOAuthRequest::RequestSignatureMethod)
Sets the signature method to be used when signing the requests.
NOTE: Currently only KQOAuthRequest::::HMAC_SHA1 is supported!
* void setHttpMethod(KQOAuthRequest::RequestHttpMethod)
Sets the HTTP method to be used for this request.
* void setAdditionalParameters(const KQOAuthParameters &additionalParams);
Sets any additional request specific parameters as specified by the
service provider.
* void setRequestBody(const KQOAuthParameters &requestParams);
Sets the request body for HTTP POST request as specified by the service
provider.
* bool isValid() const;
Returns true if this request is valid (has all the necessary parameters
required by the given request type).
* QByteArray requestBody() const;
Returns the raw request body as sent to the service provider.
* KQOAuthRequest::RequestType requestType() const;
Returns the current request type.
* QUrl requestEndpoint() const;
Returns the current request endpoint (URL location).
* void setContentType(const QString &contentType)
By default the POST requests are sent as "application/x-www-form-urlencoded".
With this method, however, the content type of the POST request can be changed.
If you want to override the content type, please look at setRawData().
* QString contentType()
Returns the current content type.
* void setRawData(const QByteArray &data);
If you send the POST request as anything else but "application/x-www-form-urlencoded"
it is assumed that you want to send some manually crafted data that kQOAuth
is not responsible of.
Set that data here.
* QByteArray rawData()
Returns the raw data you want to se send to the endpoint.
* QList<QByteArray> requestParameters();
Returns the request parameters of this request as a list of elements
sent to the service.
* void clearRequest();
Deletes all cached data from this request object.
* void setTimeout(int timeoutMilliseconds);
Sets the timeout for this request. If the timeout expires, the request will be aborted.
SOURCE CODE
============================
The source code is available on Gitorious at:
http://www.gitorious.org/kqoauth
git clone git://gitorious.org/kqoauth/kqoauth.git
EXAMPLE APPLICATION
============================
kQOAuth provides as an example application a command line Twitter client.
Currently it only supports requesting the authorization and sending status
updates, but this should be enough to show the basic usage of kQOAuth with
the convenience APIs.
See example/twittercli/ for source code.
LICENSE AND AUTHOR
============================
The library and the source code is licensed under the GNU Lesser General Public
License (LGPL). For more information, visit http://www.gnu.org/licenses/lgpl.html
This library is written by Johan Paul (johan.paul@gmail.com)
CONTRIBUTORS
============================
Keith Obenschain (obenschaink@gmail.com)
ACKNOWLEDGEMENT
============================
Thank you to Dominik Kapusta for the original Qt OAuth library, QOAuth. I wanted
to improve on this library to better suit my needs.
Visit his project at http://files.ayoy.net/qoauth/doc/
SO, WHAT'S WITH THE NAME?
============================
The library is called kQOAuth. I wanted the library to reflect the fact that it
is a Qt based library and since QOAuth was already taken, I added a 'k' in
front of it. k is for Kypeli, not KDE.
|