User Management

Last Updated on : 2024-06-05 03:15:11download

API list

Action Description
user.wx-applet.synchronization Get the user ID and access token.
user.token Generate the random token before login.
user.password.login Log in with account password.
user.refreshToken Refresh the access token granted for login.
system.userTicket Get the user ticket.
user.infos Query information about a user.
user.ticketToken Exchange the temporary ticket for the token granted for login.
user.appInfo Get information about a third-party app.
  • Method 1: To implement login with a WeChat account, call user.wx-applet.synchronization for login.
  • Method 2: To implement login to the WeChat mini program with the registered app account, perform the following steps:
    1. Call user.token.
    2. Call user.password.login to finish login.

Both login methods support user data isolation.

Get user ID and access token granted for login

API description

Get the user ID and access_token for the first time. Then, the data is cached and used during subsequent login.

access_token is used as the access token granted for login. This token must be included in all API requests that are made to access your WeChat mini program, except for the login API request.

This API has been upgraded on April 27, 2021. Before the upgrade, in the response of this API request, only uid was returned, and for other API requests, authentication with access_token was not required. After the upgrade, access_token is returned in the response of this API request.

API endpoint

action: user.wx-applet.synchronization

Request parameter

Parameter name Type Required Description
open_id String Yes Set the value to cloud.
app_schema String Yes Set the value to cloud.
nick_name String No The nickname of the WeChat account. Call wx.getUserInfo() to get the nickname.
avatar String No The avatar of the WeChat account. Call wx.getUserInfo() to get the avatar.

Sample request

{
    action: "user.wx-applet.synchronization",
    params: {
        open_id: "cloud",
        app_schema: "cloud",
        nick_name: "Nickname of WeChat account",
        avatar: "https://wechat-avatar.cn/YAIOgq83epRy7ZNvLmMUHib16hEaLgrGSxNE1BBMm",
    },
}

Response parameter

Parameter name Type Description
code Integer The response code.
success Boolean Indicates whether the operation is successful. Valid values:
  • true: success.
  • false: failure.
msg String The error message that is returned if the API call fails. This parameter value is empty if the API call succeeds.
result Object The returned result.

**Description of **result

Parameter name Type Description
uid String The user ID assigned by Tuya.
access_token String The access token granted for login.
refresh_token String The refresh token used for refreshing the access token.
expire_time String The time when the token expires.

Sample response

{
    "result": {
        "access_token": "344c7bdeb1c23b9eccc5 ****** 1edc2b",
        "refresh_token": "3783df62d6bb1fdbbb9 ****** 09f4cb4",
        "uid": "ay1582981255622T **** ",
                "expire_time":"7200"
    },
    "success": true,
    "t": 1582870559516
}

Error codes

The following table lists common error codes for the API calls. For more error codes, see Global Error Codes.

Error codes Description
500 A system error has occurred while processing your request.
1106 Invalid permission.
1109 Invalid parameter.

Generate random token before login

API description

This is the first step of login to the WeChat mini program with the registered phone number. Get the token before you call the login API.

API endpoint

action:user.token

Request parameter

Parameter name Type Description Required
country_code String The country code. For example, 86 means mainland China. Yes
username String The username. Yes
schema String The app ID of your WeChat mini program. Set the value to cloud. Yes

Sample request

{
      "action": "user.token",
      "params": {
            "schema":"cloud",
            "country_code": "86",
            "username": "1599970 **** "
          }
}

Response parameter

Parameter name Type Description
code Integer The response code. This parameter value is empty if the API call succeeds.
success Boolean Indicates whether the operation is successful. Valid values:
  • true: success.
  • false: failure.
t Long The response time.
msg String The error message that is returned if the API call fails. This parameter value is empty if the API call succeeds.
result Object LoginTokenVO

**Description of **result

Parameter name Type Description
pb_key String The Privacy-Enhanced Mail (PEM) public key for iOS.
public_key String The public key for Android.
exponent String The exponent.
token String CSRF token: used to transmit password parameters.

Sample response

{
  "result": {
    "pb_key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQ **** ",
    "public_key": "175714207292708328630951065108978120012894817439185629602411033186604670619890657753414788162628813373490070 **** ",
    "exponent": "6 **** ",
    "token":"aab1f55c71a95939d55ef01a9f84 **** ",
  },
  "success": true,
  "t": 1648176765020
}

Log in with account password

API description

This is the second step of login to the WeChat mini program with the registered phone number.

API endpoint

action:user.password.login

Request parameters

Parameter name Type Description Required
country_code String The country code. Yes
schema String Your app ID. Yes
username String The username. Yes
passwd String
  • The MD5 hash used to encrypt the plain text.
  • Use the pb_key field value returned by user.token as the public key and perform Rivest-Shamir-Adleman (RSA) encryption. This public key is applicable to both iOS and Android.
  • Convert the encrypted data into a hexadecimal string.
Yes
token String The cross-site request forgery (CSRF) token. Yes
if_encrypt Integer Specifies whether to encrypt the data. Valid values:
  • 1: yes
  • 0: no
Yes
terminal_id String The hexadecimal timestamp plus a 6-digit random number. Yes
os String The operating system. Valid values:
  • wx.mina.ios: represents WX_MINA_IOS that applies to WeChat mini programs for iOS.
  • wx.mina.android: represents WX_MINA_ANDROID that applies to WeChat mini programs for Android.
  • Yes

    Sample request

    {
      "action": "user.password.login",
      "params": {
        "country_code": "86",
        "username": "1599970 **** ",
        "passwd": "669a191672c625d3f8b30355c8660be3 **** ",
        "token": "aab1f55c71a95939d55ef01a9f84 **** ",
        "if_encrypt": 1,
        "terminal_id": "17fc003ef1b086018",
        "os": "wx.mina.ios"
      }
    }
    

    Return parameter

    Parameter name Type Description
    code Integer The response code. This parameter value is empty if the API call succeeds.
    success Boolean Indicates whether the operation is successful. Valid values:
    • true: success.
    • false: failure.
    t Long The response time.
    msg String The error message that is returned if the API call fails. This parameter value is empty if the API call succeeds.
    result Object The user session.

    Description of result

    Parameter name Type Description
    uid String User ID
    access_token String The access token.

    Sample response

    {
      "result": {
        "access_token": "3ca2d0446b74 ****** c0f2289c3b3516",
        "refresh_token": "c9b55e23f6275c2 ****** affe5efeb94989c",
        "uid": "ay1626509014612p **** ",
        "expire_time":"2695",
      },
      "success": true,
      "t": 1648176765658
    }
    

    Refresh access token granted for login

    After the value of access_token expires, in the API request that includes access_token, the following error message will be returned.

    {
      "code": 1010,
      "msg": "The token expired",
      "success": false,
      "t": 1618216767536
    }
    

    To fix the problem, call refresh_token to refresh the access token and get the new values of access_token and refresh_token.

    This API has been generally available starting from April 27, 2021.

    API endpoint

    action: user.refreshToken;
    

    Request parameter

    Parameter name Type Required Description
    refresh_token String Yes The refresh token.

    Sample request

    {
        action: "user.refreshToken",
        params: {
            refresh_token: "3783df62d6bb1fdbbb9 ****** 09f4cb4",
    
        },
    }
    

    Response parameter

    Parameter name Type Description
    code Integer The response code.
    success Boolean Indicates whether the operation is successful. Valid values:
    • true: success.
    • false: failure.
    msg String The error message that is returned if the API call fails. This parameter value is empty if the API call succeeds.
    result Object The returned result.

    **Description of **result

    Parameter name Type Description
    uid String The user ID assigned by Tuya.
    access_token String The access token granted for login.
    refresh_token String The refresh token.
    expire_time String The time when the token expires.

    Sample response

    {
      "result": {
        "access_token": "00222a5afbdcd9bccd ****** a1adb240",
        "refresh_token": "be02a06a259af6bec ****** 56814a737",
        "uid": "ay1582981255622T **** ",
        "expire_time":"7200",
      },
      "success": true,
      "t": 1618216767736
    }
    

    Get user ticket

    API description

    Get the latest ticket each time a plug-in is opened.

    API endpoint

    action: system.userTicket
    

    Request parameter

    Parameter name Type Required Description
    uid String Yes User ID

    Sample request

    {
        access_token: "344c7bdeb1c23b9eccc5 ****** 1edc2b",
        action: "system.userTicket",
        params: {
            uid: "xxxxxx",
        },
    }
    

    Response parameter

    Parameter name Type Description
    code Integer The response code.
    success Boolean Indicates whether the operation is successful. Valid values:
    • true: success.
    • false: failure.
    msg String The error message that is returned if the API call fails. This parameter value is empty if the API call succeeds.
    result Object The returned result.

    Description of result

    Parameter name Type Description
    expire_time Number The time when the token expires.
    ticket String The user ticket.

    Sample response

    {
        "result": {
            expire_time: 300
            ticket: "ST-xxx"
        },
        "success": true,
        "t": 1582870559516
    }
    

    Error codes

    The following table lists common error codes for the API calls. For more error codes, see Global Error Codes.

    Error codes Description
    500 A system error has occurred while processing your request.
    1106 Invalid permission.
    1109 Invalid parameter.

    Query user information

    API description

    Query information about a user with the ID assigned by Tuya.

    API endpoint

    action: user.infos
    

    Request parameter

    Parameter name Type Description Required
    uid String The username assigned by Tuya. Yes

    Sample request

    {
        "access_token": "344c7bdeb1c23b9eccc5 ****** 1edc2b",
        "action": "user.infos",
        "params": {
            "uid": "ay1582981255622TXro6"
        }
    }
    

    Response parameter

    Parameter name Type Description
    code Integer The response code.
    success Boolean Indicates whether the operation is successful. Valid values:
    • true: success.
    • false: failure.
    msg String The error message that is returned if the API call fails. This parameter value is empty if the API call succeeds.
    result Object The returned result.

    Description of result

    Parameter name Type Description
    uid String The user ID assigned by Tuya.

    Sample response

    {
        "result": {
        "avatar": "https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLZrASXPcUvQm3Tmia6ScRhxqoXo5NEmiac0xibFjuz4ib4bYLDibIwffp4zG6QYiclImib5jSqubTS77JEg/132",
        "create_time": 1582981255,
        "nick_name": "Nickname",
        "uid": "ay1582981255622TXro6",
        "update_time": 1582981255,
        "username": "wxa-o1Obc4oAhh_jsGGauRBs63hHpjPQYZJ"
        },
        "success": true,
        "t": 1583136691792
    }
    

    Error codes

    The following table lists common error codes for the API calls. For more error codes, see Global Error Codes.

    Error codes Description
    500 A system error has occurred while processing your request.
    1106 Invalid permission.
    1109 Invalid parameter.

    Exchange the temporary ticket for the token granted for login.

    API description

    Exchange the temporary ticket for the token granted for cloud development.

    API endpoint

    action: user.ticketToken
    

    Request parameter

    Parameter name Type Description Required
    ticket String The ticket used to temporarily authorize a user. Yes
    access_id String The value of access_id granted for cloud development. Yes

    Response parameter

    Parameter name Type Description
    code Integer The response code.
    success Boolean Indicates whether the operation is successful. Valid values:
    • true: success.
    • false: failure.
    msg String The error message that is returned if the API call fails. This parameter value is empty if the API call succeeds.
    result Object The returned result.

    Description of result

    Parameter name Type Description
    uid String The user ID assigned by Tuya.
    access_token String The access token granted for login.
    refresh_token String The refresh token for refreshing the access token.
    expire_time Integer The validity period of the token, in seconds.

    Sample request

    {
        "access_token": "344c7bdeb1c23b9eccc5 ****** 1edc2b",
        "action": "user.ticketToken",
        "params": {
            "ticket": "ST-f1d5107ebf677cb4e1a8924b5ffd94f4idjh7L9G0vv9V9zbnfWL",
        "access_id": "xxxxxxx"
        }
    }
    

    Sample response

    {
        "result": {
        "access_token": "0aa222f672e286ab076c85e6300d05d4",
        "refresh_token": "1048sads8f934f234dwidur3483dsaa3",
        "expire_time": 7200,
        "uid": "ay1582796080015nkQTp"
        },
        "success": true,
        "t": 1582870559516
    }
    

    Error codes

    The following table lists common error codes for the API calls. For more error codes, see Global Error Codes.

    Error codes Description
    500 A system error has occurred while processing your request.
    1106 Invalid permission.
    1109 Invalid parameter.

    Get third-party app information

    API description

    Get the name, logo, and description of a third-party app that is specified by schema and mobile operating system type.

    API endpoint

    action: user.appInfo
    

    Request parameter

    Parameter name Type Description Required
    ticket String The ticket used to temporarily authorize a user. Yes
    access_id String The value of access_id granted for cloud development. Yes

    Response parameter

    Parameter name Type Description
    code Integer The response code.
    success Boolean Indicates whether the operation is successful. Valid values:
    • true: success.
    • false: failure.
    msg String The error message that is returned if the API call fails. This parameter value is empty if the API call succeeds.
    result Object The returned result.

    Description of result

    Parameter name Type Description
    app_name String The name of the app.
    app_logo String The URL of the app logo.
    app_description String The description of the app.

    Sample request

    {
        "access_token": "344c7bdeb1c23b9eccc5 ****** 1edc2b",
        "action": "user.appInfo"
    }
    

    Sample response

    {
        "result": {
        "app_name": "WeChat mini program",
        "app_logo": "https://sso.tuya.com/xxxxx.png",
        "app_description": ""
        },
        "success": true,
        "t": 1582870559516
    }
    

    Error codes

    The following table lists common error codes for the API calls. For more error codes, see Global Error Codes.

    Error codes Description
    500 A system error has occurred while processing your request.
    1106 Invalid permission.
    1109 Invalid parameter.