'how to get shopee api Authorization use php5.3

i want to get shopee authorization using the source code. The language I use is php.5.3.2. No matter how many times I search, I only have a demo about the higher version, so I can't find the answer I want to ask.

In shopee, you can calculate the default signature sequence and partner key value as "HMAC-SHA256" and create an autorization value. How can I write it in code?

i already get partner key.. but i don't know how to get authorization.



Solution 1:[1]

In order to get shopee authorization, you need to prepare a header as mentioned below and send this header in curl headers with other required data. You can refer to the example given below.

    $secretKey = 'Your Secret Key' ;
    $baseUrl = "https://partner.shopeemobile.com/api/v1/";
    $action = 'The action you want to perform';
    $apiUrl = $baseUrl.$action ;
    $parameters = 'Required Parameters' ;
    $authorisation = $apiUrl."|".json_encode($parmaeters);
    $authorisation = rawurlencode(hash_hmac('sha256', $authorisation, $secretKey, 
    enter code herefalse));

    $header = array(
      'Content-Type: application/json',
      'Authorization: '.$authorisation,
    );

After preparing this, send $header in curl headers

YOUR CURL REQUEST WOULD BE LIKE THIS:

    $connection = curl_init();
    curl_setopt($connection, CURLOPT_URL, $apiUrl);
    curl_setopt($connection, CURLOPT_HTTPHEADER, $header);
    curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($connection, CURLOPT_POST, 1);
    curl_setopt($connection, CURLOPT_POSTFIELDS, json_encode($parameters));
    curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);

    $response = curl_exec($connection);
    curl_close($connection);

Hope you get your query resolved by this.

Solution 2:[2]

If you can use npm package in your php app, you can use this code:

import { createHmac } from 'crypto';
    
const timestamp = getUTCUnixTimestamps();
const baseString = `${AppID}${Apipath}${timestamp}`;
const signCode = createHmac('sha256',shopeeAppSecret).update(baseString).digest('hex');

Solution 3:[3]

To get the Shopee Auth code you'll need partnerID, partnerKey both can be seen on your Shopee Open Platform Console. Next, is the API path for Authorization which is https://partner.shopeemobile.com/api/v1/shop/auth_partner for V1 https://partner.shopeemobile.com/api/v2/shop/auth_partner for V2 both have different Auth processes

for V2

create a baseString consisting of PartnerId, API path (/api/v2/shop/auth_partner) and Unix timestamp. Create a signature using by hashing the baseString with your partnerKey.

$timest = time();
$tokenBaseString = $partnerID.$path.$timest;
$sign = hash_hmac('sha256', $tokenBaseString, $partnerKey, false);

prepare the Auth URL like this

$host = 'https://partner.shopeemobile.com'
$path = '/api/v2/shop/auth_partner'
$partner_id = 123456
$timestamp = 'unix_timestamp' //authorization url expires in 5 minutes(because of timestamp expiration)
$sign = 'the_generated_signature'
$returnURL = 'https://yourReturnURL.com'

Auth URL will look like this

$AuthURL= $host.$path.'?partner_id='.$partner_id.'?timestamp='.$timestamp.'&sign='.$sign.'&redirect'.$returnURL;

Sample Auth URL

https://partner.shopeemobile.com/api/v2/shop/auth_partner?partner_id=123456&timestamp=1648628444&sign=xxxxxxx&redirect=https://yourReturnURL.com

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Guilherme Lemmi
Solution 2 Puneet Kushwah
Solution 3