Authentication using OAuth
OAuth is an open protocol to allow secure authorization in a simple and standard method from web, mobile, and desktop applications. OAuth is used to authenticate a client against common web-services such as Google, Facebook, and Twitter.
OAuth is currently not part of a QML/JS API. So you would need to write some C++ code and export the authentication to QML/JS. Another issue would be the secure storage of the access token.
Here are some links which we find useful:
Integration example
In this section, we will go through an example of OAuth integration using the Spotify API. This example uses a combination of C++ classes and QML/JS. To discover more on this integration, please refer to Chapter 16.
This application's goal is to retrieve the top ten favourite artists of the authenticated user.
Creating the App
First, you will need to create a dedicated app on the Spotify Developer's portal.

Once your app is created, you'll receive two keys: a client id and a client secret.

The QML file
The process is divided in two phases:
The application connects to the Spotify API, which in turns requests the user to authorize it;
If authorized, the application displays the list of the top ten favourite artists of the user.
Let's start with the first step:
When the application starts, we will first import a custom library, Spotify, that defines a SpotifyAPI component (we'll come to that later).
Once the application has been loaded, this SpotifyAPI component will request an authorization to Spotify:
Until the authorization is provided, a busy indicator will be displayed in the center of the app:
The next step happens when the authorization has been granted. To display the list of artists, we will use the Model/View/Delegate pattern:
The model SpotifyModel is defined in the Spotify library. To work properly, it needs a SpotifyAPI:
The ListView displays a vertical list of artists. An artist is represented by a name, an image and the total count of followers.
SpotifyAPI
Let's now get a bit deeper into the authentication flow. We'll focus on the SpotifyAPI class, a QML_ELEMENT defined on the C++ side.
First, we'll import the <QOAuth2AuthorizationCodeFlow> class. This class is a part of the QtNetworkAuth module, which contains various implementations of OAuth.
Our class, SpotifyAPI, will define a isAuthenticated property:
The two public slots that we used in the QML files:
And a private member representing the authentication flow:
On the implementation side, we have the following code:
The constructor task mainly consists in configuring the authentication flow. First, we define the Spotify API routes that will serve as authenticators.
We then select the scope (= the Spotify authorizations) that we want to use:
Since OAuth is a two-way communication process, we instanciate a dedicated local server to handle the replies:
Finally, we configure two signals and slots.
The first one configures the authorization to happen within a web-browser (through &QDesktopServices::openUrl), while the second makes sure that we are notified when the authorization process has been completed.
The authorize() method is only a placeholder for calling the underlying grant() method of the authentication flow. This is the method that triggers the process.
Finally, the getTopArtists() calls the web api using the authorization context provided by the m_oauth2 network access manager.
The Spotify model
This class is a QML_ELEMENT that subclasses QAbstractListModel to represent our list of artists. It relies on SpotifyAPI to gather the artists from the remote endpoint.
This class defines a spotifyApi property:
An enumeration of Roles (as per QAbstractListModel):
A slot to trigger the refresh of the artists list:
And, of course, the list of artists, represented as JSON objects:
On the implementation side, we have:
The update() method calls the getTopArtists() method and handle its reply by extracting the individual items from the JSON document and refreshing the list of artists within the model.
The data() method extracts, depending on the requested model role, the relevant attributes of an Artist and returns as a QVariant:

Last updated