Search code examples
oauthmyob

Myob - AccountRight Live Api v2 Skip login screen


I am using accountright Live api v2 by MYOB. I want to get access token without going to login screen. When I send a CURL request to obtain access token i am redirected to myob login screen, how to skip that? The request I am sending is to url:

'https://secure.myob.com/oauth2/v2/authorize'

and params sent are:

Array
(
    [client_id] => xxxxxxxxxxxxxxxxxxxxxxxx
    [client_secret] => xxxxxxxxxxxxxxxxxxxxx
    [scope] => CompanyFile
    [code] => XXXXXXXXXXXXXX
    [redirect_uri] => http://myappcodeonmydomain.com
    [grant_type] => authorization_code
)

Solution

  • After your initial request to the API to get the access token, you should also be provided with a refresh token. Access tokens expire after a period of time, and need to be refreshed.

    From the Refreshing an Access Token section in the Authentication Documentation:

    Access tokens have a limited life span and when you receive one you'll also receive an Expiry Time for it and a Refresh Token. Once your access token expires it can no longer be used to access the API. So you'll need to trigger a refresh. You do this by POSTing the following parameters:

    'client_id' // your API Key 
    'client_secret' // your API Secret
    'refresh_token' // your refresh token 
    'grant_type' // this should say refresh_token
    

    To this url: https://secure.myob.com/oauth2/v1/authorize

    Note: while the data is formatted into a URL Query String you do not pass the information via the URL (that would be a GET request), you must pass the query string in the body and POST this to https://secure.myob.com/oauth2/v1/authorize

    As an example, I store my access and refresh tokens in a database, along with an expected expiry time 10 minutes in the future. If a request is going to be made after that time, I call the refresh procedure to update the access token, and am able to proceed on my merry way without needing to show the login prompt each time.

    You do need to have it shown at least once to find out which user is logging in, and the GUID of the Company File to connect to.