gpt4 book ai didi

go - 如何使用 go 获取 github api 的通知

转载 作者:IT王子 更新时间:2023-10-29 01:47:13 25 4
gpt4 key购买 nike

我正在尝试编写一个 go 程序来连接到 github api 并在我的帐户上获取通知。我可以很容易地获得用户的 repo 和信息,但我很难收到通知,即使我的帐户上有通知,数组总是空的。有人知道我做错了什么吗?提前致谢!

这是我的代码:

package main
import (
"fmt"
"log"

"golang.org/x/net/context"

"github.com/google/go-github/github"
"golang.org/x/oauth2"
)

func check(err error) {
if err != nil {
log.Fatal(err)
}
}

/**
* this function is used to get the repositories of the client
*/
func getRepo(client *github.Client) []*github.Repository {
ctx := context.Background()

repos, _, err := client.Repositories.List(ctx, "", nil)

check(err)

fmt.Printf("\n%v\n", github.Stringify(repos))

return repos
}

/**
* this function is used to get Authentification using a oauth token
*/
func getAuth() *github.Client {
ctx := context.Background()
// put your own OAUTH_TOKEN
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: "OAUTH_TOKEN"})

tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)

return client
}

/**
* this function is used to get notification on the client's account
*/
func getNotif(client *github.Client) []*github.Notification {
ctx := context.Background()

notifs, resp, err := client.Activity.ListRepositoryNotifications(ctx, "AlexandreMazgaj", "task_app", nil)

fmt.Printf("Status code of the response: %d\n", resp.StatusCode)
check(err)

return notifs
}

func main() {
// first we get authentification
client := getAuth()
// then we get the notifications
notifs := getNotif(client)

fmt.Printf("\n%v\n", github.Stringify(notifs))

}

最佳答案

我终于找到了答案,程序运行正常,问题出在我这边,我没有在我的 github 设置中启用网络通知。

关于go - 如何使用 go 获取 github api 的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47514935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com