Teambition企業內部應用開發指南

Teambition企業內部應用Python開發指南注意:此文章并非搬運,一小部分僅為借鑒 。Teambition提供了API接口,我們可以注冊成為開發者,然后通過接口獲取Teambition的數據,按照需求的格式保存和分析.
一、準備階段1.登錄Teambition企業賬號,要有管理員的權限,點擊左上角的菜單按鈕,然后點擊進入企業的“全部應用”.最后點擊“應用商店”

Teambition企業內部應用開發指南

文章插圖
Teambition企業內部應用開發指南

文章插圖
2.點擊“開放平臺”,此頁面有后期需用的API文檔,可以先收藏 。點擊“立即創建”
Teambition企業內部應用開發指南

文章插圖
3.創建企業內部應用:填寫名稱和描述4.打開應用憑證和基本信息 , 獲取ID和Secret(F12在代碼中可以直接復制)
Teambition企業內部應用開發指南

文章插圖
5.打開左側欄中的“應用開發”--“應用權限”,根據需要勾選6.打開“應用發布” , 填寫信息,發布 。
二、Python腳本編寫1.找到對應的jwt包,https://jwt.io/libraries,下載安裝(推薦PyJWT:至少我用沒有問題)2.appAccessToken的獲?。ㄗ鈧匾囊徊劍?
  • 因為文檔中沒有Python實現的任何描述,這里只提供個人寫法
from datetime import datetime, timedeltaimport jwtimport requestsimport timefrom Config import getConfig class GetTeamBitionEvents(object):def __init__(self):self.app_id = getConfig('config', 'Get_TB_Data', 'app_id')self.app_secret = getConfig('config', 'Get_TB_Data', 'app_secret')def get_aptoken(self):now_time = int(time.time())expire_time = now_time + 36000# 1 小時后超時token_dict = {'iat': now_time,'_appId': '%s' % self.app_id,'exp': expire_time,}headers = {'typ': 'jwt','alg': 'HS256'# 聲明所使用的算法}encoded = jwt.encode(payload=token_dict, key=self.app_secret, headers=headers,algorithm='HS256')# .decode('ascii')return encoded
  • 個人習慣將固定的重要數據存放在config文件中 , 你也可以直接寫入字符串
3.獲取Token后就可以開始調用API了 , 我這里將“創建項目任務類型”作為示例
  • 文檔鏈接:開放平臺文檔中心 (teambition.com)
  • 主要看六點
    • URL:https://open.teambition.com/api/v3/sfc/create
    • Method:POST
    • 權限:tb-core:sfc:create
    • 請求頭
    • 查詢參數
    • 請求體
  • 有了這些信息,就可以直接上代碼了
from __future__ import absolute_import, unicode_literalsimport requests, time, jwtclass GetTeamBitionEvents(object):def __init__(self):self.app_id = ''# 必填self.app_secret = ''# 必填self.company_url = 'https://www.teambition.com/organization/' # 固定self.company_id = ''# 一些API會用到公司IDself.callback_url = self.company_url + self.company_id# 固定self.user_id=''# 一些API會用到個人IDself.auth_url = 'https://account.teambition.com/oauth2/authorize?client_id=' + self.app_id + '&redirect_uri=' + self.callback_url# 固定def get_aptoken(self):now_time = int(time.time())expire_time = now_time + 36000# 1 小時后超時token_dict = {'iat': now_time,'_appId': '%s' % self.app_id,'exp': expire_time,}headers = {'typ': 'jwt','alg': 'HS256'# 聲明所使用的算法}encoded = jwt.encode(payload=token_dict, key=self.app_secret, headers=headers,algorithm='HS256')# .decode('ascii')return encodeddef post_proj_type(self,params,object):url = f'https://open.teambition.com/api/v3/sfc/create'app_token = (self.get_aptoken()).replace("\n", "").replace('\r', '')headers = {'Authorization': 'Bearer %s' % app_token,'X-Tenant-Id': '%s' % self.company_id,'X-Tenant-Type': 'organization','X-Operator-Id': self.user_id}return requests.post(url,json=object,params=params, headers=headers)if __name__ == '__main__':tb = GetTeamBitionEvents()projectId=tb.company_id#測試企業-項目管理副本roleId=tb.user_id# 測試角色object={'name':'測試類型'}params={'projectId':projectId}result = tb.post_proj_type(params,object)print(result.json()["result"])
  • 其中params為查詢參數,json為請求體 。根據具體API要求
    • 'params'和'object'中參數用,分隔
    • requests的方法有get/post/del/put根據需要使用
    • 參數后有勾選必填的,必須要有,不然會報錯
  • 記得打開相應權限
三、一些輔助腳本Config.pyimport configparserimport os# 讀取配置文件def getConfig(filename, section, option):""":param filename 文件名稱:param section: 服務:param option: 配置參數:return:返回配置信息"""# 獲取當前目錄路徑proDir = os.path.split(os.path.realpath(__file__))[0]# print(proDir)# 拼接路徑獲取完整路徑configPath = os.path.join(proDir, filename)# print(configPath)# 創建ConfigParser對象conf = configparser.ConfigParser()# 讀取文件內容conf.read(configPath)config = conf.get(section, option)return config

推薦閱讀