獲取使用GitHub api和Jira api Authentication的方法

 

近段時間在搭建我司的用例管理平臺,有以下需求:git

一、須要根據項目--版本--輪次的形式來管理項目用例,用例統一保存在git工程。json

二、執行用例時,若是用例執行失敗,能夠經過平臺在Jira上提bug。api

經過調研發現可使用GitHub api和Jira api,經過http調用的方式來實現,那麼獲取使用Api的Authentication做爲第一步就尤其重要。其實Git做爲代碼管理工具,Jira做爲需求、缺陷等項目與事務跟蹤工具,已被大部分公司使用,只是平時咱們沒有相似上述的需求。下面簡述下我如何使用。cookie

1、獲取Jira api  Authenticationapp

   首先:這是jira api地址:https://developer.atlassian.com/cloud/jira/platform/rest/v2/#api-rest-api-2-field-fieldKey-option-get 這個文檔中 Authentication and authorization章節介紹的獲取Authentication 都比較麻煩。curl

   其次:經過搜索我發現另一個Api介紹文檔https://developer.atlassian.com/server/jira/platform/basic-authentication/ 該文檔中Security章節中介紹了幾種訪問api的方式,我使用了Basic Authentication,這種方式相對來講最簡單、最容易理解,由於它使用的是本身登陸Jira的用戶名和密碼。下面就說明如何使用ide

   方法一:工具

      英文原文以下:ui

     Most client software for sending requests automatically generate the authorization header when you provide a username and password.加密

For example, in cURL you can specify a username and password combination with the -u argument to send a request to a Jira site

      意思就是說若是咱們提供用戶名和密碼,大多數用於發送請求的客戶端軟件會自動生成受權標頭    

      好比:curl -u username:password -X GET -H "Content-Type: application/json" http://localhost:8080/rest/api/2/issue/createmeta  便可調通api了。

       這種方式須要明文提供本身的用戶名和密碼,相對風險比較高。何況若是咱們想使用httpClient調用,這種方式也不知足要求

   方法二:

      英文原文以下:

  1. Build a string of the form username:password.
  2. Encode the string to Base64.
  3. Supply an authorization header with format Authorization: Basic {encoded-string}. Make sure to replace {encoded-string} with your encoded string from Step 2 

     意思就是經過Base64加密的方式加密username:password,而後在http請求的header中添加Basic {encoded-string}便可

    好比:curl -H "Authorization: Basic {encoded-string}" -X GET -H "Content-Type: application/json" http://localhost:8080/rest/api/2/issue/createmeta 便可調通api了

  第三:編寫本身調用httpClient的工具,在header中設置Authorization: Basic {encoded-string}便可,就能夠隨意調用Jira api。

 

2、獲取GitHub api  Authentication

    首先:git api地址:http://git.51.nb/help/api/README.md。登陸公司的git, 點擊頭像後,選擇help,在help頁面提供的文檔中選擇API。

    其次:在api首頁咱們能夠看到,官網提供了三種方式獲取Authentication:OAuth2 tokens、Personal access tokens、Session cookie。

             咱們選擇第二種方式,一樣是由於相對來講最簡單、最容易理解。

            英文原文以下:

                    一、Log in to your GitLab account.
                    二、Go to your Profile settings.
                    三、Go to Access tokens.
                    四、Choose a name and optionally an expiry date for the token.
                    五、Choose the desired scopes.
                    六、Click on Create personal access token.
                    七、Save the personal access token somewhere safe. Once you leave or refresh the page, you won't be able to access it again.

              意思就是登陸公司的git後,在頭像--settings--Access tokens 頁面,填寫name、失效時間以及使用範圍,點擊建立personal access token。建立成功後,頁面會顯示新的access token,頁面刷新後這個值就再也不顯示,因此咱們要保存在某個地方

 

                   

 

 

         

             好比:curl -H 'private-token: xxxxxxxxxxx' -X GET http://xxxxxx/api/v4/projects/xxxx  便可調通GitHub Api.

 

       第三:編寫本身調用httpClient的工具,在header中設置private-token: xxxxxxxxxxx便可,就能夠隨意調用GitHub Api。 

相關文章
相關標籤/搜索