Skip to main content

Web Authorization

This authorization method is for third-party applications to obtain user authorization to read and write user's survey data. Team developers do not need to be concerned with this.

Step 1: User Authorizes and Obtains code

Construct Web Authorization URL

https://open.wesurvey.com/connect/oauth2/authorize?appid={APPID}&redirect_uri={REDIRECT_URI}&response_type=code&scope=snsapi_user&state={STATE}#wj_redirect

Parameter Description

Parameter NameTypeRequiredDescription
appidstringYesApplication ID issued by the open platform
redirect_uristringYesCallback URL after authorization, please use urlencode to process the URL
response_typestringYesReturn type, fixed value: code
scopestringYesApplication authorization scope, for user authorization enter: snsapi_user
statestringNoWill be returned with redirect, can contain a-zA-Z0-9, max length 128 bytes
#wj_redirectYesFixed value

Note: After user clicks "Confirm", the page will redirect to redirect_uri?code={CODE}&state={STATE}


Step 2: Exchange code for Web Authorization access_token

Important Notes!

  1. The access_token obtained here is related to user resources and has a very high security level. It must be stored only on the server and cannot be passed to the client.
  2. Subsequent steps using the access_token to obtain user data must also be initiated from the server.
  3. The access_token is valid for 3 days, must re-authorize after expiration.

API Endpoint

GET https://open.wesurvey.com/api/oauth2/access_token?appid={APPID}&secret={SECRET}&code={CODE}&grant_type=authorization_code

Parameter Description

Parameter NameTypeRequiredDescription
appidstringYesApplication ID issued by the open platform
secretstringYesApplication secret issued by the open platform
codestringYesThe code returned in Step 1, becomes invalid after successfully exchanging for access_token once
grant_typestringYesFixed value: authorization_code

Response Description

Parameter NameTypeDescription
access_tokenstringAuthorization code
refresh_tokenstringUsed to refresh authorization code, valid for 30 days
expires_inintegerValid duration, default is 3 days
openidstringUnique identifier for user authorized to current application, openid can access user list and other data
{
"code": "OK",
"error": {
"type": ""
},
"data": {
"access_token":"ACCESS_TOKEN",
"refresh_token":"REFRESH_TOKEN",
"expires_in": 259200,
"openid":"OPENID"
},
"request_id": "6cda53f4-d0fa-41a9-95a0-77ea32c97f1e"
}

Step 3: Use access_token to Request User Data

Add ?appid={APPID}&access_token={ACCESS_TOKEN} to the API request
For example, to get user's survey details:

GET https://open.wesurvey.com/api/surveys/{survey_id}?appid={APPID}&access_token={ACCESS_TOKEN}

Some APIs require ?openid={OPENID} For example, to get user's survey list:

GET https://open.wesurvey.com/api/surveys?appid={APPID}&access_token={ACCESS_TOKEN}&openid={OPENID}

Other: Refresh access_token (if needed)

Since access_token has a short validity period, when it expires, you can use refresh_token to refresh it. refresh_token is valid for 30 days. After refresh_token expires, user needs to re-authorize.

API Endpoint

GET https://open.wesurvey.com/api/oauth2/refresh_token?appid={APPID}&refresh_token={REFRESH_TOKEN}&grant_type=refresh_token

Parameter Description

Parameter NameTypeRequiredDescription
appidstringYesApplication ID issued by the open platform
refresh_tokenstringYesThe refresh_token returned in Step 2
grant_typestringYesFixed value: refresh_token

Response Description

Parameter NameTypeDescription
access_tokenstringAuthorization code
expires_inintegerValid duration, default is 3 days
{
"code": "OK",
"error": {
"type": ""
},
"data": {
"access_token":"ACCESS_TOKEN",
"expires_in": 259200
},
"request_id": "6cda53f4-d0fa-41a9-95a0-77ea32c97f1e"
}

Other: Get Authorized User Information

API Endpoint

GET https://open.wesurvey.com/api/oauth2/user?appid={APPID}&access_token={ACCESS_TOKEN}&openid={OPENID}

Parameter Description

Parameter NameTypeRequiredDescription
appidstringYesApplication ID issued by the open platform
access_tokenstringYesThe access_token returned in Step 2
openidstringYesUnique identifier for user authorized to current application

Response Description

Parameter NameTypeDescription
openidstringUnique identifier for user authorized to current application
nicknamestringUser's nickname in WeSurvey system
avatarstringUser's avatar in WeSurvey system