Building a Wikipedia file Upload Service to your Google Drive using OAuth 2.0
In this tutorial, I am going to Build a Wikipedia file Upload Service to Google Drive using OAuth 2.0.
OAuth or Open Authorization is a framework simply a standard for authorization. The framework comprises how unrelated servers and services will securely allow authenticated access to their resources without any initial credentials being exchanged. OAuth has come into play with Google, Facebook, Twitter, or Microsoft accounts that can log into third-party websites without any need to provide your passwords.
We use many websites daily, in this tutorial, we use the Wikipedia document upload service to Google Drive using OAuth 2.0. Many web applications are required to register yourself into it. And all the login credentials cannot be humanly remembered. OAuth Helps prevent creating accounts and recalling passwords for each and every web.
OAuth is built upon the concept of an access token. When you authenticate yourself using a third-party web application, in a Google account. With the permission of the user, Google’s authorization server issues an access token to the web application. The access token can thus be used by the web application to access the data stored on the resource server. Therefore, OAuth is a convenient way to publish and communicate with data from protected resources. It’s also a safer and safer way for individuals to let you access their resource data. OAuth 2.0 provides web and desktop applications and mobile devices with authorization flows.
Wikipedia is one of the most detailed and up-to-date encyclopedias ever created. It is also entirely free and fully community-created.
Honestly, it is one of the most motivating projects in history, but to use it, you need to be online.
This tutorial will steer building a Wikipedia file Upload Service to your Google Drive using OAuth 2.0. If we need website information, the most straightforward way is, we mainly copied it into word files or other applications.
Finally, The big advantage is we can provide the needed document URL into our web application. Then it saves as a pdf file in google drive.
OAuth is a simple way to publish and interact with protected resource data. It’s also a safer and more secure way for people to give you access to their resource data.
Next, I’ll focus on …..
OAuth Roles
Resource Owner — The person who authorizes an application to access their account is the resource owner.
Client Application — This is an application or service that, on behalf of the resource owner, is able to make protected resource requests. The client program is a program that demands access to resources that are stored on the resource server. The client application obtains permission from the owner of the resource as well. The client framework is also known as the OAuth client application.
Authorization Server — The Authorization Server verifies the user’s identity and issues the client application with access tokens.
Resource Server — which contains the resources of a secured user account. It can accept and respond to protected resource requests.
Let’s look at …..
Abstract Flow of the Protocol
- The application requests authorization to access some protected resources in the server where the user is already authenticated and authorized.
- If the user authorized the request, the application receives an authorization grant after consent.
- The application requests an access token from the authorization server by presenting authentication of its own identity, and the authorization grant it received after user consent.
- If the client application’s identity is authenticated and the authorization grant is valid, the authorization server issues an access token to the application, completing the authorization.
- The client application requests the resource from the resource server by presenting the access token for authentication
- If the access token is valid, the resource server serves the resource to the client application.
System architecture.
This system provides OAuth2 social authentication support for applications in Django Framework.
The aim of this package is to help set up social authentication for the application. It also helps setting up the OAuth2 providers.
This package relies on python-social-auth and django-oauth-toolkit.
System architecture — Sequence diagram
Overall system architecture
How system flows
Wikipedia file Upload Service to Google Drive using the OAuth 2.0 web application, it tries to convert Wikipedia file into pdf.
It contains six main modules.
Step 1: In the first step, Visit the SaveWiki web application.
Step 2: Enter the Wikipedia URL in the given space.
Step 3: In this stage check if the user is authenticated or not.
Step 4: if the output is the same as the target, convert the Wikipedia file into pdf and Upload it.
Step 5: If the output is not equal to the target, then redirect to the google login and accept the permission to access google. Then it will flow as same as the authenticated user.
Step 6: End
Using the browser, a user moves on to a web application (OAuth client) and attempts to access a backend OData service. The web application redirects the browser to an authorization server, which asks the user to grant this web application permission to access it. An authorization code will be sent back to the web application if the user accesses this request. The obtained authorization code is exchanged by the web application for an access token on the authorization server. On behalf of the customer, the web application accesses the OData service using this access token.
The Flow
Step I: Application Registration
First, you must register your application with the service provider. This is done through a registration form in the “developer” portal of the service’s website (Google in our case), where you will provide them,
· Application Name
· Application Website
· Redirect URI or Callback URL
- Create a project in the Google developer console.
2. Enable the Google Drive API to generate your client credentials.
3. Click on Create Credentials button to start the progress.
4. Fill out the details of the client application.
5. Once completed, your client credentials will be generated.
Step II: Building the Client Application
Used Authorization Grant Type
The Authorization Code Grant is most widely used for applications on the server-side. This is a redirection-based flow, meaning the application must be able to communicate with the client browser to manage re-routes. To illustrate the OAuth functionality, the application will use the Authorization Code Grant Type.
Settings.py
Sequence diagram of Authorization Code Grant Flow:
- When run, you are presented with the screen as below.
2. Choose an account — The user needs to sign in to the account.
3. Consent page to give permissions for the application. —
When the user is redirected to the Authorization Endpoint, Google will prompt the user with the below consent page where the user would allow or cancel the permission request.
4. Every logged-in user can paste the Wikipedia link in the given space.
5. Dowloaded file
From this website, we can upload any Wikipedia file to Google drive by given the URL with this very minimalistic application.
Let’s see the code now:
The implementation of the above is shown in the below service and controller.
Obtaining the access token using an appropriate grant type
Views.py
An OAuth access token is received, invoke the resource server APIs, and perform the particular action.
Views.py
Utils.py
Wrapping up
So, we have discussed how to implement a web application that consumes the service of an OAuth Authorization Server and an OAuth Resource Server.
✌ Our implementation contains the following :
· Send a request to the OAuth authorization server website for obtaining the access token. During the flow, it will prompt for user authentication (eg: google). By supported OAuth grant type (eg: authorization code, implicit, etc.).
Once the OAuth access token is received, invoke the resource server APIs and obtain the protected resources or perform the particular action.
Got any doubts/questions/suggestions? Comment down below.
I hope you found this article useful!
Thanks for reading, if you like this post please clap for me!! 👏