英文:
How to make Github API call with token?
问题
我想进行 API 调用,列出特定用户的所有关注者。
我正在进行测试,我意识到一个 IP 可以进行的 API 调用有限制。
GitHub 文档表示,如果我使用个人令牌进行 API 调用,限制数量将会增加,但我不知道如何使用令牌进行 API 调用。
假设我的令牌是 bbbb,用户是 aaaa,那么我应该如何构建 URL?
英文:
I want to make api call which lists all the followers of certain user.
I was testing it and I realized that there are limit amount of api call that can be made by one IP.
Github document says that if I make api call with personal token, the limit number will increase but I don't know how to make api call with token.
Let's say my token is bbbb and User is aaaa, then how should I form URL?
答案1
得分: 1
要通过个人令牌调用GitHub的API以检索所有关注者的列表,请尝试以下方法,如示例代码所示(当然要使用有效的令牌)。
struct ContentView: View {
@State var followers: [UserFollower] = []
var body: some View {
List(followers) { follower in
Text(follower.login)
}
.task {
await getFollowers()
}
}
func getFollowers() async {
let token = "YOUR-TOKEN" // <--- 这里
if let url = URL(string: "https://api.github.com/user/followers") {
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.setValue("application/vnd.github.v3+json", forHTTPHeaderField: "Accept")
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
do {
let (data, _) = try await URLSession.shared.data(for: request)
followers = try JSONDecoder().decode([UserFollower].self, from: data)
}
catch {
print(error)
}
}
}
}
struct UserFollower: Identifiable, Codable {
let login: String
let id: Int
let nodeID: String
let avatarURL: String
let gravatarID: String
let url, htmlURL, followersURL: String
let followingURL, gistsURL, starredURL: String
let subscriptionsURL, organizationsURL, reposURL: String
let eventsURL: String
let receivedEventsURL: String
let type: String
let siteAdmin: Bool
enum CodingKeys: String, CodingKey {
case login, id, url, type
case nodeID = "node_id"
case avatarURL = "avatar_url"
case gravatarID = "gravatar_id"
case htmlURL = "html_url"
case followersURL = "followers_url"
case followingURL = "following_url"
case gistsURL = "gists_url"
case starredURL = "starred_url"
case subscriptionsURL = "subscriptions_url"
case organizationsURL = "organizations_url"
case reposURL = "repos_url"
case eventsURL = "events_url"
case receivedEventsURL = "received_events_url"
case siteAdmin = "site_admin"
}
}
请将 "YOUR-TOKEN" 替换为您的GitHub个人访问令牌,以便进行有效的API调用。
英文:
To make an API call to github
with a personal token to retrieve the list of all your
followers, try this approach, as shown in the example code (with a valid token of course).
struct ContentView: View {
@State var followers: [UserFollower] = []
var body: some View {
List(followers) { follower in
Text(follower.login)
}
.task {
await getFollowers()
}
}
func getFollowers() async {
let token = "YOUR-TOKEN" // <--- here
if let url = URL(string: "https://api.github.com/user/followers") {
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.setValue("application/vnd.github.v3+json", forHTTPHeaderField: "Accept")
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
do {
let (data, _) = try await URLSession.shared.data(for: request)
followers = try JSONDecoder().decode([UserFollower].self, from: data)
}
catch {
print(error)
}
}
}
}
struct UserFollower: Identifiable, Codable {
let login: String
let id: Int
let nodeID: String
let avatarURL: String
let gravatarID: String
let url, htmlURL, followersURL: String
let followingURL, gistsURL, starredURL: String
let subscriptionsURL, organizationsURL, reposURL: String
let eventsURL: String
let receivedEventsURL: String
let type: String
let siteAdmin: Bool
enum CodingKeys: String, CodingKey {
case login, id, url, type
case nodeID = "node_id"
case avatarURL = "avatar_url"
case gravatarID = "gravatar_id"
case htmlURL = "html_url"
case followersURL = "followers_url"
case followingURL = "following_url"
case gistsURL = "gists_url"
case starredURL = "starred_url"
case subscriptionsURL = "subscriptions_url"
case organizationsURL = "organizations_url"
case reposURL = "repos_url"
case eventsURL = "events_url"
case receivedEventsURL = "received_events_url"
case siteAdmin = "site_admin"
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论