Myob is popular accounting software of Australia and New Zealand. Its provides tax, accounting and other service to small and medium businesses.
Lets get started with MYOB api authentication process. To access all accounting data outside, you need to authenticate it via account credential.
To authenticate with MYOB api, you need Client ID and Secret key. Use scope as CompanyFIle and grant type as authorization_code.
Steps to Get Myob API Authentication
Step 1: Getting Code to get access token to authenticate api call. Save below code in get-data.php file.
1 2 3 4 5 6 |
<?php header("Location: https://secure.myob.com/oauth2/account/authorize?client_id=Use Client ID Here&redirect_uri=http://www.example.com/authenticate.php&response_type=code&scope=CompanyFile"); /* Redirect browser */ exit; ?> |
Step 2: After this file hit on server, you will get the code which is use for get the access token.
Use below authenticate.php to generate the token. In this file use your Myob Accountright file access credential.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
// get the genrated code in this file to create access token. $code = $_GET['code']; $url ="https://secure.myob.com/oauth2/v1/authorize"; $query="client_id=*****Use Client ID Here****&client_secret=*****Use Client Secret Here****&scope=CompanyFile&code=".$code."&redirect_uri=http://www.example.com/authenticate.php&grant_type=authorization_code"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $query);// Set the request as a POST FIELD for curl. $response = curl_exec($ch); curl_close($ch); $newarray1 = json_decode($response); $accestoken12 = $newarray1->access_token; $guid = $newarray1->user->uid; $username = 'Administrator'; $password = ''; $creential = base64_encode($username.':'.$password); $headers = array( 'Authorization: Bearer '.$accestoken12.'', 'x-myobapi-cftoken: '.$creential.'', 'x-myobapi-key: *****MOB API KEY*****', 'x-myobapi-version: v2' ); // Working Code sample for account information $ch12 = curl_init("https://api.myob.com/accountright/?format=json"); //open connection curl_setopt($ch12, CURLOPT_HTTPHEADER, $headers); //load all header data curl_setopt($ch12, CURLOPT_CUSTOMREQUEST, "GET"); curl_setopt( $ch12, CURLOPT_SSL_VERIFYPEER, 0 ); curl_setopt( $ch12, CURLOPT_SSL_VERIFYHOST, 0 ); curl_setopt( $ch12, CURLOPT_RETURNTRANSFER, 1 ); $result = curl_exec($ch12); //execute post curl_close($ch12); $newdat = json_decode($result); echo "<pre>"; print_r($newdat);After this process you have access token, but this token will expire after some time. So if you want to use API call repeatedly, than must use Refresh Token method. |
This will generate new access token every time using created Refresh Token. In this way, no need to authenticate it via account login credential again an again.
can you please check what the issue i tried same as u did but getting
[error] => invalid_request
regards
Hi, when you get this error?