欢迎使用爱卡Open平台


概述

授权

为第三方应用提供访问爱卡论坛功能的授权服务

代理

提供访问论坛API的代理服务

应用申请

写邮件

1. 填写应用名称及网址等信息

2. 发送给: sun.xianwei@xcar.com.cn

审核

1. 管理员收到申请邮件,符合条件的应用,在管理后台添加

2. 将使用样例(xcar_open_client.zip)及生成的client_id, client_secret 发给应用申请者

样例

1. 管理员会以邮件方式发送样例(xcar_open_client.zip)

2. 根据样例,修改其中参数

登录流程

登录按钮

1. 在页面上添加登录爱卡帐号的按钮

2. 模仿xcar_open_client.zip包中的auth_code.php,修改相应参数

3. 将生成的$authorization_url,设置为登录按钮的跳转链接

用户授权

1. 用户点击登录按钮,跳转到爱卡Open授权页面

2. 输入爱卡用户名、密码进行授权

3. 爱卡Open验证用户名、密码成功后,将授权码返回给应用

获取令牌

1. 应用得到授权码以后,要模仿auth_code.php中的方法

2. 用POST方式向爱卡Open换取令牌(access_token)

3. 令牌在有效期内不需要重复申请,可本地保存

4. 令牌过期可重新申请授权,换取令牌,或模仿refresh_token.php更新令牌

注册流程

获取令牌

1. 模仿push_user.php中的方法获取令牌

2. 此令牌在有效期内不需要重复申请,可本地保存

3. 此令牌过期只能重新获取令牌,不能更新令牌

添加用户

1. 模仿push_user.php中的方法使用令牌,推送用户数据

2. 爱卡Open返回 uid, username, mobile 等信息

测试

测试

完整示例

示例

前提

假设您的域名是: http://somesite.domain.com
使用https及指定端口号的例子: https://somesite.domain.com:1234
端口号不能超过65535

应用申请

发邮件给Open管理员:sun.xianwei@xcar.com.cn
邮件标题:【Open应用申请】XXX应用申请使用Open平台
邮件内容:
    应用名称:XXX
    应用网址:http[s]://somesite.domain.com[:port]

审批并回复

Open管理员在应用审批完成后,回复申请邮件,
其中包括应用的 client_id 及 client_secret
以及使用样例(目前仅提供PHP版本): xcar_open_client.zip

设置本地环境

将 xcar_open_client.zip 解压到WEB可访问的路径
修改 auth_code.php 中的 client_id 及 client_secret
并把 redirect_uri 指向 http://somesite.domain.com/path/to/auth_code.php
修改其他的PHP文件,同样修改 client_id, client_secret 及 redirect_uri
注意URL是指向所打开的PHP,而不都是指向 auth_code.php

请求数据

获取code

server : https://open.xcar.com.cn/Authorize
request: GET
array(
    'response_type'     => 'code',
    'approval_prompt'   => 'auto',
    'client_id'         => $client_id,
    'redirect_uri'      => $redirect_uri,
    'state'             => $random_state,
);

response:
http://somesite.domain.com/path/to/auth_code.php?code=3a151bc4d3dfd622f694ee8511b561911ac39433&state=677e1843b6a51c5f0175a5e20d77878312ecc510

获取token

server: https://open.xcar.com.cn/Authorize/token
request: POST
array(
    'grant_type'       => 'authorization_code',
    'client_id'        => $client_id,
    'client_secret'    => $client_secret,
    'redirect_uri'     => $redirect_uri,
    'code'             => $code
);

response:
array(
    'access_token'     => 'c1089a2faa716a47be008b30722253c97f1c9bce',
    'refresh_token'    => 'f92ca2ed0cc30c62b41c07ab5282dd536f4d817c',
    'token_type'       => 'Bearer',
    'expires_in'       => 3600
);

添加用户

1. 第一步

server: https://open.xcar.com.cn/Authorize/client_credentials
request: POST
array(
    'grant_type'        => 'client_credentials',
    'client_id'         => $client_id,
    'client_secret'     => $client_secret,
);

response:
array(
    'access_token'     => '861076a45a1fe47395d924f99dec67b9533364b8',
    'token_type'       => 'Bearer',
    'expires_in'       => 3600
);

2. 第二步

server: https://open.xcar.com.cn/Register
request header:
array(
    "Authorization: {$data['token_type']} {$data['access_token']}",
);
request: POST
array(
    // 接受国内手机号(11位数字)
    'mobile'        => '18618363511',
    // 接受英文、数字、中文组合,不能超过20字符
    'username'      => 'gh_testuser',
    'password'      => '1280434702734',
    'user_ip'       => '123.123.123.123',
);

response:
array(
    'uid'       => $uid,
    'username'  => $bbs_username,
    'mobile'    => $mobile,
);

测试

授权测试 - 用户名 + 密码登录授权

访问 http://somesite.domain.com/path/to/auth_code.php
页面跳转到授权页,填写爱卡帐户用户名+密码,点击授权按钮
页面展示

access token: c1089a2faa716a47be008b30722253c97f1c9bce
refresh token: f92ca2ed0cc30c62b41c07ab5282dd536f4d817c
token type: Bearer
expires in: 3600
Array ( [success] => 1 [message] => You accessed my APIs! )

表示调用成功,获得授权码

授权测试 - 手机号 + 密码登录授权

访问 http://somesite.domain.com/path/to/auth_code.php
页面跳转到授权页,填写爱卡帐户手机号+密码,点击授权按钮
页面展示

access token: c1089a2faa716a47be008b30722253c97f1c9bce
refresh token: f92ca2ed0cc30c62b41c07ab5282dd536f4d817c
token type: Bearer
expires in: 3600
Array ( [success] => 1 [message] => You accessed my APIs! )

表示调用成功,获得授权码

授权测试 - 错误的用户名、手机号或密码错误

访问 http://somesite.domain.com/path/to/auth_code.php
页面跳转到授权页,填写 错误的 爱卡帐户用户名、手机号+密码,点击授权按钮
页面展示(将会改成页面展示)

{message: "用户名或密码错误"}

注册测试 - 未传递手机号或为空

修改 push_user.php 文件中 31 行开始的用户数组中的值,使mobile的值为''
访问 http://somesite.domain.com/path/to/push_user.php
返回相同手机号用户信息,如下:

access token: 861076a45a1fe47395d924f99dec67b9533364b8
token type: Bearer
expires in: 3600
Array ( [message] => 缺少手机号 )

注册测试 - 手机号在爱卡已存在

修改 push_user.php 文件中 31 行开始的用户数组中的值,使mobile的值为一个爱卡系统中已有的手机号
访问 http://somesite.domain.com/path/to/push_user.php
返回相同手机号用户信息,如下:

access token: 1f8b03d323608297a331e070129fd48086f697e6
token type: Bearer
expires in: 3600
Array ( [uid] => 915242 [username] => radision [mobile] => 18618363511 )

注册测试 - 手机号不存在

修改 push_user.php 文件中 31 行开始的用户数组中的值,使mobile的值为一个爱卡系统中不存在的手机号
访问 http://somesite.domain.com/path/to/push_user.php
返回相同手机号用户信息,如下:

access token: 1f8b03d323608297a331e070129fd48086f697e6
token type: Bearer
expires in: 3600
Array ( [uid] => 1023915242 [username] => gh_xxxxxxxxxx [mobile] => 186183635119 )

反馈问题

有任何问题,可直接打我电话: 18618363511
或邮件: sun.xianwei@xcar.com.cn 联系,谢谢!