PHP Classes

How Can the PHP OAuth Client Class Support New OAuth Servers - PHP OAuth Library package blog

Recommend this page to a friend!
  All package blogs All package blogs   PHP OAuth Library PHP OAuth Library   Blog PHP OAuth Library package blog   RSS 1.0 feed RSS 2.0 feed   Blog How Can the PHP OAuth...  
  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  

Author:

Viewers: 717

Last month viewers: 20

Package: PHP OAuth Library

OAuth is a flexible protocol that allows accessing APIs on behalf of users of a given service.

Despite OAuth is a standard, accessing a new OAuth server requires to create custom code to access the respective API or configure a generic OAuth client like this PHP OAuth client class to adapt its behaviour to access OAuth server using specific configuration values.

Read this article to learn how to support a new OAuth server just by adding a few lines to the JSON configuration file that this PHP OAuth client users.




Loaded Article

In this article you will learn:

1. What is the PHP OAuth Library

2. What the PHP OAuth Library Needs to Work

3. Which Are the Most Important Options to Configure to Support a New Server

4. Where Can I Find the Values to Set the Configuration Variables

5. How to Configure the OAuth Client Class

6. Real Example of a Configuration File

7. Getting Support, Downloading the OAuth Client Package, or Installing it with the PHP Composer Tool


1. What is the PHP OAuth Library

The PHP OAuth Library is a package that can send HTTP requests to API Web servers using the OAuth protocol. It is able to retrieve access tokens to send those HTTP request on behalf of users of a site that provides an API.

For instance, you can send email messages on behalf of Gmail user by running a PHP script that uses the PHP OAuth library to call the Gmail API.

2. What the PHP OAuth Library Needs to Work

The PHP OAuth library needs to send HTTP requests to OAuth-based API Web servers. Therefore it uses a HTTP client class to send HTTP requests to API Web servers.

If you install the PHP OAuth library using the PHP Composer tool, the HTTP client library is automatically installed. If you just downloaded the PHP OAuth library as a compressed archive, you need to download and install also the HTTP client class.

You can find instructions to download or install the HTTP client class in its package page.

3. Which Are the Most Important Options to Configure to Support a New Server

The PHP OAuth Client class supports many types of configuration variables. You just may need to configure just a few options to support a new OAuth server. Here follows the most important variables that you need to configure:

oauth_version

Set this option to the version of the OAuth protocol that the servers supports. Most current OAuth servers use version 2.0. Older servers may still support versions like 1.0 or 1.0a.

dialog_url

Set this option to the URL that the user browser will be redirected in the first step of the protocol, which is the step that the user will authorize your application to access the API of the OAuth server.

This option should be set to a URL that is a template. This means that the URL has variable values that the OAuth client class will replace with values before redirecting the user browser to the to pass parameters that are specific to your application. Here is an example of the dialog URL:

https://oauthserverdomain.com/oauth2/authorize?client_id={CLIENT_ID}&redirect_uri={REDIRECT_URI}&response_type=code&state={STATE}&scope={SCOPE}

You do not have to change these template variables placeholders in the URL. The OAuth client class will do that for you at run time.

Here is the meaning of some of the dialog URL template variables:

  • {CLIENT_ID} - Identifier of your application that your created before using your application eventually accessing the site of the OAuth server to manage client applications

  • {STATE} - Internal value used by the OAuth client class to identify the user that is authorizing to access the OAuth server API.

  • {SCOPE} - List of scopes of the OAuth server API that you may be request.

  • {REDIRECT_URI} - URL of your application site to where the user browser will be redirected when the user authorizes (or not) your application to access the OAuth server API on his behalf.

access_token_url

Set this option to the URL of the OAuth server API that should be used by the OAuth client class to retrieve a token value that will be used to call the API.

4. Where Can I Find the Values to Set the Configuration Variables

The values that should be used to configure the access to the API are usually in the site that documents that API.

5. How to Configure the OAuth Client Class

The PHP OAuth client class is a generic class that can be used to access many different OAuth based servers. All that is needed to support a new server is to set a few configuration values that are different from server to server.

Configuration Class Variables

One way to configure the values to access a new OAuth server is to change the values of the variables of the OAuth client class object. For instance, if the OAuth server supports version 2.0 of the protocol, you can configure the OAuth protocol version like this: 

$client->oauth_version = "2.0";

The oauth_configuration.json Configuration File

A better way to set all configuration variables in a file named oauth_configuration.json. This is text file that defines the structure and values of an object stored in JSON format.

This file may contain the configuration values to access one or more types of OAuth servers.

Using a configuration file is more convenient, as you do not need to change your application code to make it use different OAuth servers.

6. Real Example of a Configuration File

The OAuth client package comes with a configuration file that defines values to access many types of OAuth servers. You can either add the values for a new server, or just remove all entries for all servers and add the values for that server.

Here follows an example of the contents of the oauth_configuration.json file to specify the values to access a OAuth server named Polar.

{
 "servers":
 {
  "Polar":
  {
   "oauth_version": "2.0",
   "dialog_url": "https://flow.polar.com/oauth2/authorization?response_type=code&scope={SCOPE}&client_id={CLIENT_ID}&redirect_uri={REDIRECT_URI}&state={STATE}",
   "access_token_url": "https://polarremote.com/v2/oauth2/token",
   "access_token_authentication": "basic",
   "store_access_token_response": true
  }
 }
}

7. Getting Support, Downloading the OAuth Client Package, or Installing it with the PHP Composer Tool

You can use the code and configuration files of the OAuth client class by going to the download page and retrieve its archive.

You can also install the package using the PHP Composer tool by following the instructions in the installation page.

If you have comments or support questions that you want to ask to clarify your doubts to use this package, feel free to post a comment using the form below, or go to this package support forum.




You need to be a registered user or login to post a comment

1,611,091 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:



Comments:

No comments were submitted yet.



  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  
  All package blogs All package blogs   PHP OAuth Library PHP OAuth Library   Blog PHP OAuth Library package blog   RSS 1.0 feed RSS 2.0 feed   Blog How Can the PHP OAuth...