fabric Report API
1、Token生成
接口 : post https://fabric.io/oauth/token 请求头:Headers Content-Type : application/json 正文: body { "grant_type":"password", "scope":"organizations apps issues features account twitter_client_apps beta software answers", "username":"mimimimimi@qq.com", //登录名 "password":"123456789", //登录密码 "client_id":"2c18f8a77609ee6bbac9e53f3768fedc45fb96be0dbcb41defa706dc57d9c931", "client_secret":"092ed1cdde336647b13d44178932cba10911577faf0eda894896188a7d900cc9" }
2、返回值
{ "access_token": "ccccccccccccccccccccccc", "token_type": "bearer", "expires_in": 86400, "refresh_token": "refresh_tokenqqqqqqqqqqqqq", "scope": "organizations apps issues features account twitter_client_apps beta software answers" }
利用 access_token 可以进行后续接口访问。
3、利用 refresh_token 刷新新的token(讲得到的refresh_token<无过期时间>保存下来,刷新token):
接口 : post https://fabric.io/oauth/token 请求头:Headers Content-Type : application/json 正文: body { "grant_type":"refresh_token", "refresh_token":"refresh_tokenqqqqqqqqqqqqq" }
返回值
{ "access_token": "ccccccccccccccccccccccc", "refresh_token": "refresh_tokenqqqqqqqqqqqqq" }
4、请求接口 接口文档出处 https://github.com/strongself/fabricio/blob/develop/docs/api_reference.md
举例两个接口
(1)、GET - https://fabric.io/api/v2/apps
get https://fabric.io/api/v2/apps Headers Authorization: Bearer {access_token}
返回值
1 [ 2 { 3 "id": "11111111", 4 "name": "8888888888", 5 "bundle_identifier": "包名", 6 "base_identifier": "8888888888", 7 "collect_analytics": true, 8 "created_at": "2016-08-01T09:03:47Z", 9 "analytics_app_has_received_data": true, 10 "analytics_forward_to_google_analytics": false, 11 "analytics_include_purchase_events_in_forwarded_events": false, 12 "platform": "android", 13 "status": "activated", 14 "latest_build": null, 15 "icon_url": "https://s3.amazonaws.com555555555icon.png", 16 "icon_hash": null, 17 "kit_versions": null, 18 "sdk_kits": null, 19 "map_of_available_products": null, 20 "firebase_crashlytics": false, 21 "icon32_url": "https://s3.amazonaws.com/assets.crashlytics.com//icon.png", 22 "icon64_url": "https://s3.amazonaws.com/assets.crashlytics.com/production//icon.png", 23 "icon128_url": "https://s3.amazonaws.com/assets.crashlytics.com/production//icon.png", 24 "accounts_count": 23, 25 "organization_id": "1111111111111", 26 "watched": null, 27 "importance_level": null, 28 "app_link": null, 29 "dashboard_url": "https://www.fabric.io/333333333333", 30 "impacted_devices_count": 0, 31 "unresolved_issues_count": 0, 32 "crashes_count": 0 33 } 34 ]
*** 将红色的记录下来 organization_id 和 app_id ***
(2)获取日活
接口 get https://fabric.io/api/v2/organizations/organization_id/apps/app_id/growth_analytics/daily_active.json?start=1478736000&end=1478736000 头设置 Headers Authorization: Bearer {access_token}
返回值,解析需要的值:
{
"build": "all",
"series": [
[
1478736000, //日期
0 //日活
],
[
1478822400,
0
],
[
1481328000,
0
]
],
"start": 1478736000,
"end": 1481328000,
"app_id": "appid--------",
"deltas": {
"week_over_week": {
"delta_fraction": null,
"last_week_value": null
}
}
}
完整代码 PHP
1 <?php 2 3 class ScriptUserDaily 4 { 5 //保存第一次获取的 refresh_token ,用于下次刷新token用 6 private $filePath = '/files/fabricToken.json'; 7 8 public function fire() 9 { 10 $access_token = $this->getRefreshToken(); 11 if(empty($access_token)) 12 $access_token = $this->getToken(); 13 14 $edata = time(); 15 $sdata = $edata - 24 * 3600 * 5; 16 17 $header = [ 18 "Authorization: Bearer ".$access_token 19 ]; 20 21 //数据库获取应用,主要获取 organization_id 和 app_id 22 $appinfo = FanAppInfo::byFabric()->get(); 23 foreach ($appinfo as $appItem){ 24 $fabricid = $appItem->fabricid; 25 if(empty($fabricid)) 26 continue; 27 28 $organization_id = $appItem->organization_id; 29 $url = "https://fabric.io/api/v2/organizations/$organization_id/apps/$fabricid/growth_analytics/daily_active.json?start=$sdata&end=$edata"; 30 31 $this->getDatas($url,$header,1); 32 33 $url2 = "https://fabric.io/api/v2/organizations/$organization_id/apps/$fabricid/growth_analytics/daily_new.json?start=$sdata&end=$edata"; 34 35 $this->getDatas($url2,$header,2); 36 } 37 } 38 39 private function getRefreshToken(){ 40 //获取 refresh_token 从文件中读取保存的refresh_token 41 $path = $this->filePath; 42 43 $JsonData = file_get_contents($path); 44 45 $rejson = json_decode($JsonData, true); 46 $refresh_token = $rejson['refresh_token']; 47 48 //刷新 token 49 $url = 'https://fabric.io/oauth/token'; 50 $header = [ 51 "content-type: application/json" 52 ]; 53 $body = [ 54 'grant_type' => 'refresh_token', 55 'refresh_token' => trim($refresh_token) 56 ]; 57 58 $data = $this->curl_post($url,$header,json_encode($body)); 59 $rejson = json_decode($data, true); 60 61 $access_token_new = ''; 62 $refresh_token_new = ''; 63 if(isset($rejson['refresh_token'])) 64 $refresh_token_new = $rejson['refresh_token']; 65 if(isset($rejson['access_token'])) 66 $access_token_new = $rejson['access_token']; 67 68 if(!empty($refresh_token_new)){ 69 $txt = [ 70 'access_token' => $access_token_new, 71 'refresh_token' => $refresh_token_new 72 ]; 73 74 //重新写入新的 refresh_token 75 $this->writeRefreshToken($txt); 76 } 77 78 return $access_token_new; 79 } 80 81 private function getToken(){ 82 $url = 'https://fabric.io/oauth/token'; 83 $header = [ 84 "content-type: application/json" 85 ]; 86 $body = [ 87 'grant_type' => 'password', 88 'scope' => 'organizations apps issues features account twitter_client_apps beta software answers', 89 'username' => '14141414@qq.com', 90 'password' => '123456789', 91 'client_id' => '2c18f8a77609ee6bbac9e53f3768fedc45fb96be0dbcb41defa706dc57d9c931', 92 'client_secret' => '092ed1cdde336647b13d44178932cba10911577faf0eda894896188a7d900cc9' 93 ]; 94 95 $data = $this->curl_post($url,$header,json_encode($body)); 96 $rejson = json_decode($data, true); 97 98 $access_token_new = ''; 99 $refresh_token_new = ''; 100 if(isset($rejson['refresh_token'])) 101 $refresh_token_new = $rejson['refresh_token']; 102 if(isset($rejson['access_token'])) 103 $access_token_new = $rejson['access_token']; 104 105 if(!empty($refresh_token_new)){ 106 $txt = [ 107 'access_token' => $access_token_new, 108 'refresh_token' => $refresh_token_new 109 ]; 110 111 //重新写入新的 refresh_token 112 $this->writeRefreshToken($txt); 113 } 114 115 return $access_token_new; 116 } 117 118 private function writeRefreshToken($txt){ 119 $path = $this->filePath; 120 121 $myfile = fopen($path, "w"); 122 $txt = json_encode($txt); 123 fwrite($myfile,$txt); 124 fclose($myfile); 125 } 126 127 private function getDatas($url,$header,,$type){ 128 $resData = $this->curl_get($url,$header); 129 $datas = json_decode($resData, true); 130 131 if(!isset($datas['series'])) 132 return ''; 133 134 $active = 0; 135 $news = 0; 136 foreach ($datas['series'] as $item){ 137 $date = date('Y-m-d',$item[0]); 138 139 if($type == 1){ 140 $active = intval($item[1]); 141 }elseif($type == 2){ 142 $news = intval($item[1]); 143 } 144 145 //处理数据 146 147 } 148 } 149 150 private function curl_get($url, $header = [], $time = 5){ 151 $ch = curl_init(); 152 curl_setopt($ch, CURLOPT_URL, $url); 153 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 154 curl_setopt($ch, CURLOPT_HEADER, 0); 155 if (!empty($header)) { 156 curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 157 } 158 curl_setopt($ch, CURLOPT_TIMEOUT, $time); 159 $result = curl_exec($ch); 160 curl_close($ch); 161 return $result; 162 } 163 164 private function curl_post($url, $header = [], $body = [], $time = 5){ 165 $ch = curl_init(); 166 curl_setopt($ch, CURLOPT_URL, $url); 167 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 168 if (!empty($body)) { 169 curl_setopt($ch, CURLOPT_POSTFIELDS, $body); 170 } 171 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 172 curl_setopt($ch, CURLOPT_HEADER, 0); 173 if (!empty($header)) { 174 curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 175 } 176 curl_setopt($ch, CURLOPT_TIMEOUT, $time); 177 $result = curl_exec($ch); 178 curl_close($ch); 179 return $result; 180 } 181 }
*** 参考文档 https://github.com/strongself/fabricio/blob/develop/docs/api_reference.md ***