Get OAuth 2.0 access token using Retrofit 2.x

Retrofit is an awesome and easy to use library. You can use Retrofit for any Android or Java application which need to interact with our OAuth2.0 server to get access token.

You just need to build the retrofit properly to get it done. It doesn’t require any advance knowledge. Let’s see how it can be done easily.

To get the access token using Retrofit we need to do –

Add Retrofit in your project via Maven or Gradle. Complete installation guide can be found here.

Now let’s create the Retrofit builder. And before doing that we need to create a service interface.

import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

public interface AccessTokenServiceInterface {

    Call<TokenResponse> getToken(@Field("client_id") String client_id, @Field("client_secret") String client_secret, @Field("scope") String scope, @Field("grant_type") String grant_type);

And now the builder.

import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class Http {
    public static void main(String[] args) {
        Retrofit retrofit = new Retrofit.Builder()

        AccessTokenServiceInterface service = retrofit.create(AccessTokenServiceInterface.class);

        //grant types = client_credentials
        Call<TokenResponse> call = service.getToken("OAUTH CLIENT ID", "OAUTH CLIENT SECRET", "basic email", "client_credentials");
        try {
            Response<TokenResponse> response = call.execute();
        } catch (IOException e) {

And also we need to map the JSON response from our OAuth 2.0 server. So we need a model to map that. Let’s create model class


public class TokenResponse {

    private String tokenType;
    private Integer expiresIn;
    private String accessToken;

    public String getTokenType() {
        return tokenType;

    public void setTokenType(String tokenType) {
        this.tokenType = tokenType;

    public Integer getExpiresIn() {
        return expiresIn;

    public void setExpiresIn(Integer expiresIn) {
        this.expiresIn = expiresIn;

    public String getAccessToken() {
        return accessToken;

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;


Now run Http.main and you will get your access token.

That’s it. Now run the Http.main and you will get the access token easily. Download these scripts from our PreviewTechnologies/access-token-retrofit GitHub repository.

This article also available on our support portal.

cURL vs “file_get_contents()” in Google App Engine – Performance analysis

We all know that to do any kinds of external HTTP request from your PHP application (deployed in Google App Engine a.k.a GAE) we have three options basically. PHP native cURL extension, “cURL Lite” provided by Google and the native http:// and https:// PHP stream handlers.

cURL requires a valid billing profile and only you can enable it in your Google Cloud paid project. And that’s why Google’s custom cURL Lite actually use Google’s urlfetch service that you can use in your application free version.

But recent days, our engineering team was just wondering which can be little bit faster among cURL or cURL Lite or PHP native PHP HTTP handler, in this sense little bit faster meaning we also count even 50ms latency. That’s why I was running some test with a single script hosted on Google App Engine (PHP Standard Runtime environment). We had lots of PHP microservice apps hosted on Google App Engine and all services at a certain time needs to talk each other via HTTP external request. But sometimes, we were aware that latency is killing some communication.

So we just built 2 files basically in PHP. One is using cURL to post some foobar json data to an external URL ( and another one was using the native http:// and https:// PHP stream handlers. Let’s see what was our experimental scripts look like.


 * Using CURL

$data = array("foo" => "bar");
$data_string = json_encode($data);

$ch = curl_init('');
//$ch = curl_init('/post.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string)

$result = curl_exec($ch);

 * php_http_stream.php
 * using cURL lite. It use google's urlfetch service

$url = '';
$data = array("foo" => "bar");
$data_string = json_encode($data);

$contentLength = strlen($data_string);

$headers = "accept: */*\r\n" .
    "Content-Type: application/json\r\n" .
    "Content-Length: $contentLength\r\n";

$context = [
    'http' => [
        'method' => 'POST',
        'header' => $headers,
        'content' => $data_string,
$context = stream_context_create($context);
$result = file_get_contents($url, false, $context);

And here is the trace report of those two call.

@0 ms
Name			RPCs	Total Duration (ms)
/curl_lite.php		1	450
/urlfetch.Fetch		1	333
Timestamp		xxxx-xx-xx (
Traced time 		333 ms
Untraced time 		117 ms

http/response/size	25
@0 ms
Name				RPCs	Total Duration (ms)
/curl.php			1	753
/remote_socket.Close		4	4
/remote_socket.Connect		10	157
/remote_socket.CreateSocket	4	10
/remote_socket.GetSocketOptions	1	1
/remote_socket.Poll		10	469
/remote_socket.Receive		2	2
/remote_socket.Send		2	2
/remote_socket.SetSocketOptions	1	1
Timestamp	2017-xx-xx (
Traced time 	646 ms
Untraced time 	107 ms

http/response/size		25

So what does it mean to you? It means a lot for me. Obviously cURL Lite is saving me couple milliseconds. And also I don’t need to be afraid of my “socket operation” quota that was used in cURL.

So in this, what should I say? file_get_contents() is more optimized? Of course, I am just talking how it’s performing for little external URL call with Google’s urlfetch service.

So if your application needs to interact with external service with less configuration and options, then I would prefer to use native PHP HTTP stream handler and make all external http call with file_get_contents() function. file_get_contents() use urlfetch service and you don’t need to enable cURL extension in your application.

Changing File Permissions in cPanel

Permissions can be very important when it comes to hosting your website. Permissions can allow our server computer to write and edit your files. Along with that, some files need to be protected from writing and editing, as a security measure. You can change your file permissions in many ways.

Step 1: Open your File Manager and navigate to the file or folder that you need to change.

Step 2: Click on the name of the file or folder.

Step 3: Click on the Change Permissions link in the top menu of the File Manager page.

Step 4: Click on as many check boxes as you require to create the right permission. The permission numbers underneath the check boxes will update automatically.

Step 5: Click on the Change Permissions button when you are ready. The new permission level is saved and the display updated to show the modified file.

Setting up PHP, MySQL, phpmyadmin on mac sierra

Mac OS Sierra comes with PHP, MySQL pre installed isn’t it cool? There are two-label root directory, one is called system label and another is user label. The default system label root is “/Library/WebServer/Documents”. The other user label root is missing by default. In this article we’re going to work with user label root.

PHP installation

Create folder named “Sites” at your root directory. (For those who are newbies) From terminal type “cd” and “sudo mkdir Sites”. To run this user label system make a configuration file under /etc/apache2/users/{your-username}.conf (here in the place of “your-username” type your currently logged in users username) and place the below code in it.

<Directory "/Users/username/Sites/">
AllowOverride All
Options Indexes MultiViews FollowSymLinks
Require all granted


permission this file should be “-rw- r– r– 1 root” if not, run this command “sudo chmod 644 {your-username}.conf”

Next open the “/etc/apache2/httpd.conf” file to active some module

✓ LoadModule authz_core_module libexec/apache2/
✓ LoadModule authz_host_module libexec/apache2/
✓ LoadModule userdir_module libexec/apache2/
✓ LoadModule include_module libexec/apache2/
✓ LoadModule rewrite_module libexec/apache2/
✓ LoadModule php5_module libexec/apache2/
✓ Include /private/etc/apache2/extra/httpd-userdir.conf

Then open another apache configuration file at “/etc/apache2/extra/httpd-userdir.conf “ and active module

✓ Include /private/etc/apache2/users/*.conf

Save and restart apache “sudo apachectl restart”

If your are going the web serving document root at “/Library/WebServer/Documents”, its good idea to allow any .htaccess file used to override the default settings. This can be done by editing the “/etc/apache2/httpd.conf” file “AllowOverride All”. Also uncomment the below module

✓ LoadModule rewrite_module libexec/apache2/

To test php running write a php file under /Library/WebServer/Documents and name it according your wish and run through browser “localhost/{your-file- name.php}”

MySQL installation

Download MySQL from MySQL’s native website and install it manually.
The first time after installations a dialog box will popup showing a temporary password, save that carefully for future use. Stop MySQL and start in safe mood and change the given password to yours by running these below commands.

✓ sudo /usr/local/mysql/support-files/mysql.server stop
✓ sudo mysqld_safe — skip-grant- tables
✓ mysql -u root
✓ ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘MyNewPass’;
✓ sudo /usr/local/mysql/support-files/mysql.server start

You may be encountering the problem/error “ERROR! MySQL server PID file could not be found!”.

The problem is because the MySQL pid file isn’t in the MySQL configuration file. The default configuration sample file directory is “/usr/local/mysql/support-files/my-default.cnf”

Create a MySQL configuration file copy and pasting from “my-default.cnf tomy.cnf” And the PID file is under the directory “/usr/local/mysql/data/{your-pc-name}.pid”. At the bottom of configuration file write the MySQL pid path like “pid-file = /usr/local/var/mysql/{username}.pid”. look for running MySQL using command “ps -A | grep MySQL” and kill it “sudo kill -15 pidno” if any.
start MySQL “/usr/local/mysql/bin/mysql -v -uroot -p” here you will be promoted for password. Type the temporary password given/shown. First change the password “ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘MyNewPass’;”

To work with MySQL through terminal “/usr/local/mysql/bin/mysql -u root –p”


Download phpmyadmin, from your download directory rename it phpmyadmin and put it under (you created earlier) Sites folder Make a directory under phpmyadmin and name it config “Sudo mkdir ~/Sites/phpmyadmin/config”

Change the permission “chmod o+w ~/Sites/phpmyadmin/config”

Browse http://localhost/~{your-username}/phpmyadmin/setup/

Click on “new server” and then on Authentication tab fill the “password for config auth” with your desired password. Click save and then save. move the file under the config directory to direct phpmyadmin folder (one level up) and remove the config directory.

Browse http://localhost/~username/phpmyadmin/

Adding Custom Validation Rules in Valitron

Valitron is a simple, minimal and elegant stand-alone validation library with NO dependencies. Valitron uses simple, straightforward validation methods with a focus on readable and concise syntax. Valitron is the simple and pragmatic validation library we’ve been looking for.

Adding Custom Validation Rules :

To add your own validation rule, use the addInstanceRule method with a rule name, a custom callback or closure, and a error message to display in case of an error. The callback provided should return boolean true or false.

such as :

$validation->addInstanceRule(‘functionName’, function () {
return false;

$validation->rule(‘functionName’, ‘fieldName’)->message(‘Error Message’);


We can also use the following structure :

if (condition) {

$validation->addInstanceRule(‘functionName’, function () {
return false;

$validation->rule(‘functionName’, ‘fieldName’)->message(‘Error Message’);