首页 > 用户手册 > 导出报表数据——OAuth认证

导出报表数据——OAuth认证

1.生成OAuth Secret

  • 在 /manage_app 页面选定要查看数据所属的应用,以“数极客演示”应用为例,appKey = '8dea3eba4ea2018e'
  • 点击“生成OAuth Secret”按钮,获取secretKey= '882109bca55d4e28ad8ff9c564897e69'


a. 出于安全原因,secretKey不会保存,每次点击按钮生成后请妥善保存,关闭获取窗口后不会再展示,如果忘记或丢失请重新生成

b. SaaS服务请求地址_SJK_API_PATH_https://a.shujike.com/api-v2

2.申请OAuth access_token

shell命令行示例代码:

curl -d 'client_id=你的APPKEY&client_secret=你的client_secret&grant_type=client_credentials&scope=basic
' https://a.shujike.com/api-v2/oauth/clientCredentials

PHP示例代码:

$url = "https://a.shujike.com/api-v2/oauth/clientCredentials";
$data=array(
        'client_id' => '你的APPKEY',
        'client_secret' => '你的client_secret',
        'grant_type' => 'client_credentials',
        'scope' => 'basic'
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, http_build_query($data) );
$output = curl_exec ( $ch );
curl_close ( $ch );

其中client_id字段即appKey,client_secret字段对应第一步生成的secretKey,后面的grant_typescope参数保留默认值不变即可

返回数据为json格式,示例如下:

{
  "access_token": "你的TOKEN",
  "expires_in": 3600,
  "token_type": "Bearer",
  "scope": "basic"
}

access_token有效期1小时,过期后需要重新进行步骤2生成新的access_token

3.调用数据接口

4.返回结果

以json形式返回数据,格式如下:

{
  "status": 0,
  "msg": {
    "msg": "success",
    "desc": ""
  },
  "data": [
  ...
  ]
字段 含义 说明
status api返回结果状态 0为成功,如返回非0结果,请对照错误码查看问题
msg api返回结果信息 如成功,返回'success',否则会输出具体错误原因
data api返回数据结果 以数组形式返回,详细说明见具体api文档

示例: 报表以事件分析—默认浏览次数、人数、注册用户数的报表为例 条件如下图所示

shell示例代码:

curl -d 'access_token=你的TOKEN&events[0][name]=浏览&events[0][fe_name]=view&events[0][metrics][0][name]=event&events[0][metrics][0][op]=&events[0][metrics][1][name]=unique_event&events[0][metrics][1][op]=&events[0][metrics][2][name]=unique_userid&events[0][metrics][2][op]=&events[0][relation]=and&dimensions[]=channel_type&relation=and&granularity=hour&time_range[]=2019-08-07&time_range[]=2019-08-07' https://a.shujike.com/api-v2/event/calculate_grid

PHP示例代码:

$url = "https://a.shujike.com/api-v2/event/calculate_grid";
$data=array(
        'access_token' => '你的TOKEN',
        'events' => array(
            array(
                'name' => '浏览',
                'fe_name' => 'view',
                'metrics' => array(
                    array(
                        'name' => 'event',
                        'op' => 
                    ),
                    array(
                        'name' => 'unique_event',
                        'op' => 
                    ),
                    array(
                        'name' => 'unique_userid',
                        'op' => 
                    ),
                ),
                'relation' => 'and'
            ),
        ),
        'dimension' => array(
            'channle_type'
        ),
        'relation' => 'and',
        'granularity' => 'hour',
        'time_range' => array(
            '2019-08-07',
            '2019-08-07'
        )
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, http_build_query($data) );
$output = curl_exec ( $ch );
curl_close ( $ch );

返回结果:

{
  "status": 0,
  "msg": {
    "msg": "success",
    "desc": ""
  },
  "data": [
    [
      "2019-08-07 00:00:00",
      "\u641c\u7d22\u5f15\u64ce",
      "13",
      "12",
      "0"
    ],
    [
      "2019-08-07 00:00:00",
      "\u76f4\u63a5\u8bbf\u95ee",
      "131",
      "69",
      "0"
    ],
    [
      "2019-08-07 00:00:00",
      "\u793e\u4ea4\u5a92\u4f53",
      "1",
      "1",
      "0"
    ],
    [
      "2019-08-07 01:00:00",
      "\u76f4\u63a5\u8bbf\u95ee",
      "137",
      "57",
      "0"
    ],
    [
      "2019-08-07 01:00:00",
      "\u5916\u94fe",
      "1",
      "1",
      "0"
    ]
  ]
}

详细说明见(http://www.shujike.com/document/api-v2.0_event.html)

results matching ""

    No results matching ""