Account and Interface Integration Solutions
Introduction
When using WeSurvey, enterprise internal systems or platform products often focus on how to integrate account systems and organizational structures, and how to access WeSurvey from other platforms without login.
This document provides you with deep integration solutions for WeSurvey, allowing tight integration with your system through embedding or redirection, combined with open APIs.
Objectives
- Synchronize third-party accounts to WeSurvey
- Enable seamless access to WeSurvey from third-party systems
Solution Diagram
Scenario 1: Enterprise Internal System Integration (Premium Version)
Benefits
Enterprise internal systems can be communities, HR portals, IM tools, etc., with existing user systems and organizational structures. You can achieve:
- Organizational structure and account integration: After logging into the enterprise internal account, users can directly use WeSurvey without logging in again with QQ/WeChat accounts
- Multiple enterprise members can collaboratively manage survey editing, distribution, and data statistics, or manage survey assets according to organizational structure
- Centralized distribution of internal surveys, limiting respondent scope, or internal 360-degree peer reviews
- After employees leave, they cannot access WeSurvey with enterprise accounts, ensuring data security
Integration Steps
- [Required] Obtain API credentials (team level): First upgrade your team to Premium Version in WeSurvey, and check AppID and Secret in Team Management - Team Information
- [Required] Use AppID and Secret to obtain access_token API Authorization
- [Required] Use access_token to register a WeSurvey account for enterprise members
- [Required] Use access_token to get team information
- [Required] Use access_token to add current account to team
- [Required] Use access_token to get one-time login code for current account
- [Required] Use one-time login code to redirect or embed, entering WeSurvey workspace or survey response page Unified Entry
- [Optional] If you forget the survey-side user_id, query through Get Third-party User Information API
- [Optional] Complete synchronization of team organizational structure Sync Organization Structure
Scenario 2: Service Platform Integration (Service Provider Partnership)
Benefits
Service platforms can be ToC communities, productivity tools, or ToB marketing platforms, procurement systems, etc., providing services to different organizations or individuals. You can achieve:
- Embed WeSurvey as a tool in your platform
- Users can directly use WeSurvey after logging into your platform, without logging in again with QQ/WeChat accounts
- Combined with Webhook, achieve real-time data push to third-party platforms with original system user identifiers, enabling feature linkage and data closed-loop
- If your platform serves multiple enterprises, you can also implement service provider development mode to provide WeSurvey integration services for enterprises
Integration Steps
- [Required] Obtain API credentials (platform level): Please contact customer service to discuss your platform requirements. After reaching cooperation intention, we will provide you with exclusive AppID and Secret
- [Required] Use AppID and Secret to obtain access_token API Authorization
- [Required] Use access_token to register a WeSurvey account for platform users
- [Required] Use access_token to create a team space for current account
- [Required] Use access_token to get one-time login code for current account
- [Required] Use one-time login code to redirect or embed, entering WeSurvey workspace or survey response page Unified Entry
- [Optional] If you forget the survey-side user_id, query through Get Third-party User Information API
Authorization Flow
Code Examples
- index.html <iframe> embeds redirect.php
- PHP initiates backend request to register third-party user
- PHP initiates backend request to get one-time login code
- PHP 302 redirects to WeSurvey Unified Entry
index.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<title>Embedding Demo</title>
</head>
<body>
<h1>Survey editor page with login state is embedded in iframe below</h1>
<div>
<iframe src="./redirect.php" allowfullscreen="allowfullscreen" sandbox="allow-same-origin allow-scripts" width="2000" height="1000"></iframe>
</div>
</body>
</html>
redirect.php
<?php
class redirection
{
private $_appid;
private $_access_token;
public function __construct($appid, $access_token)
{
$this->_appid = $appid;
$this->_access_token = $access_token;
}
/**
* Request WeSurvey Open API
* @param $method
* @param $url
* @param $data
* @return mixed
*/
private function openapi_request($method, $url, $data)
{
$params = [
"appid" => $this->_appid,
"access_token" => $this->_access_token,
];
if ($method == "GET" && !empty($data)) {
$params = array_merge($params, $data);
$data = null;
}
// Assemble request parameters
$params_string = urldecode(http_build_query($params));
$api = "https://" . $url . "?" . $params_string;
$headers = ['Content-Type:application/json', 'Accept:application/json'];
try {
// Initiate request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($method == "POST") {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
}
$output = curl_exec($ch);
curl_close($ch);
} catch (Exception $e) {
return false;
}
// Result processing
$result = json_decode($output, true);
if ($result['code'] != "OK") {
die($result['error']['type']);
}
return $result["data"];
}
/**
* @param $openid string Third-party unique user identifier
* @return int Survey-side user_id
*/
/**
* @param $openid
* @param $nickname
* @param $avatar
* @return mixed
*/
function create_user($openid, $nickname, $avatar)
{
$method = "POST";
$url = "open.wesurvey.com/api/sso/users";
$data = [
'openid' => $openid,
'nickname' => $nickname,
'avatar' => $avatar,
];
$response = $this->openapi_request($method, $url, $data);
return $response['user_id'];
}
/**
* @param $user_id
* @return mixed
*/
function get_login_code($user_id)
{
$method = "POST";
$url = "open.wesurvey.com/api/sso/code";
$data = [
'user_id' => $user_id,
];
$response = $this->openapi_request($method, $url, $data);
return $response['code'];
}
}
// 1. Apply for appid and secret
// 2. Use appid and secret to obtain access_token, fill in here
$appid = "";
$access_token = "";
$openid = ""; // Fill in integrator's unique user identifier
$nickname = ""; // Fill in integrated user nickname
$avatar = ""; // Fill in integrated user avatar
$ins = new redirection($appid, $access_token);
// 3. Request WeSurvey Open API to create user
$user_id = $ins->create_user($openid, $nickname, $avatar);
// 4. Request WeSurvey Open API to get one-time login code
$code = $ins->get_login_code($user_id);
// 5. Redirect to WeSurvey page with appid + code
$action = trim($_GET['action']);
switch ($action) {
case 'survey_create':
// Redirect to WeSurvey creation page
$url = "https://wesurvey.com/api/v2/redirect?appid={$appid}&code={$code}&action={$action}";
break;
case 'survey_collect':
// Redirect to WeSurvey response page
$survey_id = 0; // Get from survey distribution link
$survey_hash = "abcd";
$url = "https://wesurvey.com/api/v2/redirect?appid={$appid}&code={$code}&action={$action}&survey_id={$survey_id}&survey_hash={$survey_hash}";
break;
default:
echo "unknown action";
exit();
}
header('Location: ' . $url);