調用Github API時需要進行身份認證。Github建議並提供基於OAuth2的第三方認證。
一、使用github提供的第三方認證(最常用方法)
使用github提供的第三方認證,需要先註冊0auth應用。按要求填寫Homepage URL、Authorization callback URL
官方指南:https://developer.github.com/v3/guides/basics-of-authentication/
參考博客:https://blog.csdn.net/javagaorui5944/article/details/52918772
思路:用戶在應用中點擊登錄->應用跳轉到github官方提供的授權頁->用戶同意授權->通過回調URL跳回到應用並提供token->程序用token進行下一步操作
二、Basic認證+OAuth Token手動實現第三方認證
由於小程序是封閉的環境,不支持外部鏈接跳轉,基本不支持H5頁面,所以需要嘗試其他方法。
根據官方指南此API原本是給用戶的,讓用戶能管理應用程序對該用戶賬戶的訪問權限。
其中一個接口能夠讓用戶通過basic驗證創建特定應用的授權。對客戶端來說,在不支持外部鏈接(如小程序)的情況下,剛好可以用來手動實現某特定應用的第三方認證。
此方法需要先在github setting中註冊OAuth Application,拿到應用的client_id和client_secret。客戶端獲取用戶的用戶名和密碼,再通過basic認證的方式,使用該接口獲取github認證token。這樣既繞開github的第三方認證跳轉,又能使用OAuth認證。
//請求信息
{
url: https://api.github.com/authorizations/clients/{#client_id}
method:put
headers:{
Authorization: 'Basic '+ base64轉碼(username:password),
Accept: application/vnd.github.v3+json //github建議在Accept中指定版本
}
body:{
"client_secret": client_secret,
"scopes": [
"public_repo"
],
"note": "admin script"
}
}
如果收到的response爲404,官網指南:其他認證方法中提到,這表示請求未通過身份認證。一般情況下,應該檢查請求的method、authorization、body信息是否有誤。
如果收到如下response則OAuth認證成功。
//response
{
"id": 22696****,
"url": "https://api.github.com/authorizations/22696****",
"app": {
"name": "test-liang",
"url": "http://localhost:80",
"client_id": client_id
},
"token": "",
"hashed_token": "9abc92d192d2b5cb43213c0b2ef63facb243aabcdbcd421fd1e10e********",
"token_last_eight": "6a975b37",
"note": "admin script",
"note_url": null,
"created_at": "2018-10-11T15:17:03Z",
"updated_at": "2018-10-11T15:17:03Z",
"scopes": [
"public_repo"
],
"fingerprint": null
}
需注意:出於安全性的考慮,github從15年開始不推薦使用token,並且只有新建授權的情況下返回的token字段纔有數據,再次訪問拿到的token是空字符串。如下圖在已經授權的情況下請求,則收到的token字段爲空。
目前是可以正常使用這個API返回的token,但是需要專門建一個後臺存儲token。否則會出現BUG:前端清理了token緩存,但是github方面沒有取消授權,本地拿不到token,github API也不返回token,邏輯判斷時會一直卡在登錄頁。
在application setting頁可以看到你的應用多了一個user。證明手動實現第三方認證成功。
然後就可以使用token進行下一步信息獲取。這種方法獲取的token對應上文【一】方法的access_token。官方指南提供了2種使用token獲取認證的方法,作爲請求參數或請求頭。
如圖無需用戶名,只要有token則可以獲取用戶信息。
三、Basic認證直接獲取數據
此方法與Oauth認證無關。是用過basic認證直接向Github API獲取用戶信息。有一定風險,GitHub建議只在demo中使用,而不在生產環境中使用。
要使用GitHub API進行基本身份驗證,只需發送與該帳戶關聯的用戶名和密碼即可
參考鏈接:https://www.aliyun.com/jiaocheng/490770.html
注:推薦使用POSTMAN進行請求測試。方便直觀,可自動實現Basic Authrization轉碼
//request
{
url:https://api.github.com/user
method:get
headers:'Basic '+ base64轉碼(username:password)
}
看到plan字段,代表驗證成功
{
"login": "yellowpig",
"id": 2659****,
"node_id": "MDQ6VXNlcjI2NTkyODQ5",
"avatar_url": "https://avatars2.githubusercontent.com/u/26592849?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/yellowpig",
"html_url": "https://github.com/yellowpig",
"followers_url": "https://api.github.com/users/yellowpig/followers",
"following_url": "https://api.github.com/users/yellowpig/following{/other_user}",
"gists_url": "https://api.github.com/users/yellowpig/gists{/gist_id}",
"starred_url": "https://api.github.com/users/yellowpig/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/yellowpig/subscriptions",
"organizations_url": "https://api.github.com/users/yellowpig/orgs",
"repos_url": "https://api.github.com/users/yellowpig/repos",
"events_url": "https://api.github.com/users/yellowpig/events{/privacy}",
"received_events_url": "https://api.github.com/users/yellowpig/received_events",
"type": "User",
"site_admin": false,
"name": null,
"company": null,
"blog": "",
"location": null,
"email": null,
"hireable": null,
"bio": null,
"public_repos": 10,
"public_gists": 0,
"followers": 2,
"following": 4,
"created_at": "2017-03-22T07:31:18Z",
"updated_at": "2018-10-11T10:36:02Z",
"private_gists": 0,
"total_private_repos": 0,
"owned_private_repos": 0,
"disk_usage": 57995,
"collaborators": 0,
"two_factor_authentication": false,
"plan": {
"name": "free",
"space": 976562499,
"collaborators": 0,
"private_repos": 0
}
}