Call oauth java web service using php help

Call oauth java web service using php

This demo is created for the php developers who are facing much problem to call oauth java web service using php. Here my effort is to compare java client with php client for call oauth webservice. Here final out come is oauth signature using provided credentials. It will be containing X-Authorization string with oauth_timestamp, oauth_nonce, oauth_signature.

eg. x-authorization=oauth_timestamp=”XXX”,oauth_nonce=”YYY”,oauth_signature=”ZZZ”

I will mention some of the methods side by side for java and php.

in generate we are provided with below credentials from web service provider:

    public $APP_ID = "MYAPP";
    public $CONSUMER_KEY = "MYCONSUMERKEY";
    public $SECRET_KEY = "SECRETKEY";
    public $SIGNATURE_METHOD = "HmacSHA1";

So first things comes in oauth is time stamp, method for java/php is as below:

public function getTimestamp() {  // PHP
    $curdate = time();
    return $curdate;
}
private static String getTimestamp() { // Java
    return Long.toString((System.currentTimeMillis() / 1000));
}

Next method is: getNonce : its basically random screen :

public function generateRandomString($length = 32) { // PHP
        //return "69jep9z8F1ZGjEht8mU7EtjwNxFirvso";
        $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXZY0123456789';
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, strlen($characters) - 1)];
        }
        return $randomString;
    }
private static String getNonce() { // Java
    return RandomStringUtils.randomAlphanumeric(32);
}

and last important method I am putting here is:

public static String generateHmacSHA1Signature(String data, String key)
            throws GeneralSecurityException { // Java
        System.out.println("\n\nBefore HmacSHA1 : " + data);
        byte[] hmacData = null;

        try {
            SecretKeySpec secretKey = new 
                           SecretKeySpec(key.getBytes("UTF-8"),
                                 SIGNATURE_METHOD);
            Mac mac = Mac.getInstance(SIGNATURE_METHOD);
            mac.init(secretKey);
            hmacData = mac.doFinal(data.getBytes("UTF-8"));
            StringBuilder sb = new StringBuilder();
            for (byte b : hmacData) {
                sb.append(String.format("%02X", b));
            }
            String hash = sb.toString();
            String base64Encoded = new Base64().encodeAsString(hmacData);
            return new Base64().encodeAsString(hmacData);
        } catch (UnsupportedEncodingException e) {            
            throw new GeneralSecurityException(e);
        }
    }
public function generateHmacSHA1Signature($encodedParams) { // PHP
        $key = utf8_encode($this->SECRET_KEY);
        $encodedParams = utf8_encode($encodedParams);
        $digestLength = $this->CC_SHA1_DIGEST_LENGTH;
        $CCHmac = hash_hmac("sha1", $encodedParams, $key, TRUE);
        $HMAC = substr($CCHmac, 0, $digestLength);        
        $hash = base64_encode($HMAC);
        return $hash;
}

usage in php will be as below:

$ob_oauth = new OAuthSignature();
$oauth = $ob_oauth->generateConsumerSignature();

I am attaching two java files and one php file in the zip  for java code to run we will require to jar files commons-lang3-3.1.jar and commons-codec-1.8.jar

Let me know if not getting any points here. Greetings!!

Download zip: oauthFiles-Php-Java Zip Download,

Free SEO Checker |
Test your website for free with OnAirSEO.com

Get Your SEO report!

Don’t miss the next post!

Loading

Related Posts