# Giới thiệu

## 1App.vn Open API

1App.vn provides tokens to synchronize data between the services of 1App.vn and other systems.

## Steps to get tokens

#### Created app

1. Click link following to create the app: <https://open.1app.vn/app/list>
2. Click button add app.

* App name: Your app name.
* Return data url: Url receives the returned data after synchronization: order, product, customer,...
* Verified key: Your security key, which will be returned with the returned Url, used for verifying the returned data

#### Get accessCode

1. Redirect link to 1app.

* <https://open.1app.vn/oauth?appId={YOUR\\_APPID}\\&redirectLink={YOUR\\_REDIRECT\\_LINK}\\&businessId={YOUR\\_BUSINESSID}>

| Param        | Data Type | Mandatory | Description                                                                   |
| ------------ | --------- | --------- | ----------------------------------------------------------------------------- |
| appId        | int       | Yes       | Your app id                                                                   |
| redirectLink | string    | Yes       | The link receives the accessCode returned                                     |
| businessId   | int       | No        | If you have many businesses, and want each business to have a different token |

#### Get accessToken

**Request**

* Url request: <https://open.1app.vn/oauth/access\\_token>
* RESTful applications use HTTP requests to post data. The POST params include:

| Param      | Data Type  | Mandatory | Description          |
| ---------- | ---------- | --------- | -------------------- |
| appId      | int        | Yes       | Your app id          |
| secretKey  | string(64) | Yes       | Your app secret key  |
| accessCode | string(64) | Yes       | Your app access code |

**Response**

```javascript
  {
    "code": 1, // 1 = success, 0 = failed (see errors in messages)
    "messages": { // if the code = 0 the server will return error messages
        "error code": "message 1"
    },
    "accessToken": accessToken
  }
```

**Code Sample**

* Simple request: send product information

```php
<?php

    $appId = "_YOUR_APPID_";
    $secretKey = "_YOUR_SECRET_KEY_";
    $accessCode = "_YOUR_ACCESS_CODE_";

    $postArray = array(
        "appId" => $appId,
        "secretKey" => $secretKey,
        "accessCode" => $accessCode
    );

    $curl = curl_init("https://open.1app.vn/oauth/access_token");
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postArray);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $curlResult = curl_exec($curl);

    if(! curl_error($curl)) {
        // success
        $response = json_decode($curlResult);
    } else {
        // failed, cannot connect nhanh.vn
        $response = new stdClass();
        $response->code = 0;
        $response->message = curl_error($curl);
    }
    curl_close($curl);

    if ($response->code == 1) {
        // get access token successfully
        echo $response->accessToken;
    } else {
        // failed, show error messages
        echo $response->message;
    }
```
