如何使用令牌调用GitHub API?

huangapple go评论88阅读模式
英文:

How to make Github API call with token?

问题

我想进行 API 调用,列出特定用户的所有关注者。
我正在进行测试,我意识到一个 IP 可以进行的 API 调用有限制。
GitHub 文档表示,如果我使用个人令牌进行 API 调用,限制数量将会增加,但我不知道如何使用令牌进行 API 调用。
如何使用令牌调用GitHub 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.
如何使用令牌调用GitHub API?

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 = &quot;YOUR-TOKEN&quot;  // &lt;--- here
        if let url = URL(string: &quot;https://api.github.com/user/followers&quot;) {
            var request = URLRequest(url: url)
            request.httpMethod = &quot;GET&quot;
            request.setValue(&quot;application/vnd.github.v3+json&quot;, forHTTPHeaderField: &quot;Accept&quot;)
            request.setValue(&quot;application/json; charset=utf-8&quot;, forHTTPHeaderField: &quot;Content-Type&quot;)
            request.setValue(&quot;Bearer \(token)&quot;, forHTTPHeaderField: &quot;Authorization&quot;)
            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 = &quot;node_id&quot;
        case avatarURL = &quot;avatar_url&quot;
        case gravatarID = &quot;gravatar_id&quot;
        case htmlURL = &quot;html_url&quot;
        case followersURL = &quot;followers_url&quot;
        case followingURL = &quot;following_url&quot;
        case gistsURL = &quot;gists_url&quot;
        case starredURL = &quot;starred_url&quot;
        case subscriptionsURL = &quot;subscriptions_url&quot;
        case organizationsURL = &quot;organizations_url&quot;
        case reposURL = &quot;repos_url&quot;
        case eventsURL = &quot;events_url&quot;
        case receivedEventsURL = &quot;received_events_url&quot;
        case siteAdmin = &quot;site_admin&quot;
    }
}

huangapple
  • 本文由 发表于 2023年5月29日 14:32:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355122.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定