本文整理汇总了Golang中golang.org/x/oauth2.StaticTokenSource函数的典型用法代码示例。如果您正苦于以下问题:Golang StaticTokenSource函数的具体用法?Golang StaticTokenSource怎么用?Golang StaticTokenSource使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了StaticTokenSource函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GenerateGithub
func GenerateGithub(name string, token string, exclude string) map[string]*Repo {
repos := make(map[string]*Repo)
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
tc := oauth2.NewClient(oauth2.NoContext, ts)
client := github.NewClient(tc)
opt := &github.RepositoryListByOrgOptions{
Type: "all",
ListOptions: github.ListOptions{PerPage: 100},
}
for {
newRepos, resp, err := client.Repositories.ListByOrg(name, opt)
check(err)
for _, newRepo := range newRepos {
url := fmt.Sprintf("[email protected]:%s/%s.git", name, *newRepo.Name)
repos[*newRepo.Name] = &Repo{
Url: url,
}
}
if resp.NextPage == 0 {
break
}
opt.ListOptions.Page = resp.NextPage
}
return repos
}
示例2: RegisterResult
//RegisterResult registers the supplied result
func (githubClient *GitHubClient) RegisterResult(result Result) error {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: githubClient.Token},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
client := github.NewClient(tc)
if githubClient.BaseUrl != nil {
client.BaseURL = githubClient.BaseUrl
}
log.Info("Submitting result")
repositories := client.Repositories
status, _, err := repositories.CreateStatus(
githubClient.From,
githubClient.Repo,
result.SHA,
&github.RepoStatus{
State: github.String(result.State),
TargetURL: github.String(result.Url),
Description: github.String(result.Message),
Context: github.String("continuous-integraion/walter"),
})
log.Infof("Submit status: %s", status)
if err != nil {
log.Errorf("Failed to register result: %s", err)
}
return err
}
示例3: main
func main() {
flag.Usage = usage
flag.Parse()
cachingHTTPClient = util.CachingHttpClient()
if *tokenFlag == "" || cachingHTTPClient == nil {
flag.Usage()
return
}
ds = clientFlags.CreateDataset()
if ds == nil {
flag.Usage()
return
}
defer ds.Store().Close()
if err := clientFlags.CreateProgressFile(); err != nil {
fmt.Println(err)
} else {
defer clientFlags.CloseProgressFile()
}
token := oauth2.Token{AccessToken: *tokenFlag}
authHTTPClient = oauth2.NewClient(oauth2.NoContext, oauth2.StaticTokenSource(&token))
start = time.Now()
var user = getUser()
printStats(user)
userRef := ds.Store().WriteValue(user)
fmt.Printf("userRef: %s\n", userRef.TargetRef())
_, err := ds.Commit(userRef)
d.Exp.NoError(err)
}
示例4: getClient
func (d *Driver) getClient() *godo.Client {
token := &oauth2.Token{AccessToken: d.AccessToken}
tokenSource := oauth2.StaticTokenSource(token)
client := oauth2.NewClient(oauth2.NoContext, tokenSource)
return godo.NewClient(client)
}
示例5: getClient
func getClient(token string) *github.Client {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
return github.NewClient(tc)
}
示例6: gistHandler
func gistHandler(w http.ResponseWriter, r *http.Request) {
log.Debug("gistHandler()")
// get access token
c, err := r.Cookie("access_token")
if err != nil {
log.Error("get cookie error: ", err)
http.Error(w, "Github Auth Error", http.StatusInternalServerError)
return
}
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: c.Value},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
// github api
client := github.NewClient(tc)
gist, err := makeGist(r.Body)
if err != nil {
log.Errorf("make gist error: %v", err)
http.Error(w, "Server Error", http.StatusInternalServerError)
return
}
retGist, _, err := client.Gists.Create(&gist)
if err != nil {
log.Error("create gist error: ", err)
http.Error(w, "Post Gist Error", http.StatusInternalServerError)
return
}
io.WriteString(w, *retGist.HTMLURL)
}
示例7: retrieveCommits
// retrieveCommits fetches the latest deployed commits as well
// as the latest GitHub commits for a given Project.
// it will also check if the user has permission to pull.
func retrieveCommits(r *http.Request, project goship.Project, deployUser string) (goship.Project, error) {
// define a wait group to wait for all goroutines to finish
var wg sync.WaitGroup
githubToken := os.Getenv(gitHubAPITokenEnvVar)
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: githubToken})
client := github.NewClient(oauth2.NewClient(oauth2.NoContext, ts))
for i, environment := range project.Environments {
for j, host := range environment.Hosts {
// start a goroutine for SSHing on to the machine
wg.Add(1)
go getCommit(&wg, project, environment, host, deployUser, i, j)
}
wg.Add(1)
go getLatestGitHubCommit(&wg, project, environment, client, project.RepoOwner, project.RepoName, i)
}
// wait for goroutines to finish
wg.Wait()
for i, e := range project.Environments {
project.Environments[i] = e
for j, host := range e.Hosts {
host.GitHubCommitURL = host.LatestGitHubCommitURL(project)
host.GitHubDiffURL = host.LatestGitHubDiffURL(project, e)
host.ShortCommitHash = host.LatestShortCommitHash()
project.Environments[i].Hosts[j] = host
}
}
u, err := getUser(r)
if err != nil {
log.Printf("Failed to get user %s", err)
}
return filterProject(project, r, u), err
}
示例8: create
func create(desc string, pub bool, files map[string]string) (*github.Gist, error) {
ghat := getAccessToken()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: ghat},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
client := github.NewClient(tc)
f := make(map[github.GistFilename]github.GistFile)
for k := range files {
_k := github.GistFilename(k)
f[_k] = github.GistFile{Content: github.String(files[k])}
}
gist := &github.Gist{
Description: github.String(desc),
Public: github.Bool(pub),
Files: f,
}
gist, _, err := client.Gists.Create(gist)
return gist, err
}
示例9: initializeClient
func initializeClient(token string) {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
client = github.NewClient(tc)
}
示例10: Login
// Logs in to github using oauth.
// Returns error if login fails.
func Login(userkey string) error {
file, err := ioutil.ReadFile(*Path + ".issue/config.json")
if err != nil {
GitLog.Println("open config: ", err)
os.Exit(1)
}
temp := new(Config)
if err = json.Unmarshal(file, temp); err != nil {
GitLog.Println("parse config: ", err)
os.Exit(1)
}
Conf = temp
token := Conf.Token
if Conf.Secure {
key := []byte(userkey)
token = encrypt.Decrypt(key, token)
}
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
client = github.NewClient(tc)
user, _, err := client.Users.Get("")
if err != nil {
GitLog.Printf("\nerror: %v\n", err)
return err
}
log.Printf("\nLogged into: %v\n", github.Stringify(user.Login))
GitLog.Print("Logged into: ", github.Stringify(user.Login))
return nil
}
示例11: BuildHTTPClient
func BuildHTTPClient(proxy, token string) *http.Client {
var base http.RoundTripper
base = http.DefaultTransport
// Proxy layer
if len(proxy) > 0 {
u, _ := url.Parse(proxy)
base = &http.Transport{
Proxy: http.ProxyURL(u),
}
}
// Authentication layer
if len(token) > 0 {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
base = &oauth2.Transport{
Source: ts,
Base: base,
}
}
// Rate limiting
transport := &RateLimitedTransport{
Base: base,
}
return &http.Client{
Transport: transport,
}
}
示例12: cmdSetup
func cmdSetup(c *cli.Context) {
token := c.Args().First()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
client := qiita.NewClient(tc)
user, err := client.AuthenticatedUser.Show()
if err != nil {
fmt.Println("Auth failed")
os.Exit(1)
}
loadConfig()
viper.Set("accessToken", token)
viper.Set("id", user.Id)
err = saveConfig()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Token saved")
}
示例13: NewClient
// NewClient returns a new BitBalloon API client
func NewClient(config *Config) *Client {
client := &Client{}
if config.BaseUrl != "" {
client.BaseUrl, _ = url.Parse(config.BaseUrl)
} else {
client.BaseUrl, _ = url.Parse(defaultBaseURL)
}
if config.HttpClient != nil {
client.client = config.HttpClient
} else if config.AccessToken != "" {
client.client = oauth2.NewClient(
oauth2.NoContext,
oauth2.StaticTokenSource(
&oauth2.Token{
AccessToken: config.AccessToken,
},
),
)
}
if &config.UserAgent != nil {
client.UserAgent = config.UserAgent
} else {
client.UserAgent = userAgent
}
client.Sites = &SitesService{client: client}
client.Deploys = &DeploysService{client: client}
return client
}
示例14: createGithubClient
func (grp githubRegistryProvider) createGithubClient(credentialName string) (*http.Client, *github.Client, error) {
if credentialName == "" {
return http.DefaultClient, github.NewClient(nil), nil
}
c, err := grp.cp.GetCredential(credentialName)
if err != nil {
log.Printf("Failed to fetch credential %s: %v", credentialName, err)
log.Print("Trying to use unauthenticated client")
return http.DefaultClient, github.NewClient(nil), nil
}
if c != nil {
if c.APIToken != "" {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: string(c.APIToken)},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
return tc, github.NewClient(tc), nil
}
if c.BasicAuth.Username != "" && c.BasicAuth.Password != "" {
tp := github.BasicAuthTransport{
Username: c.BasicAuth.Username,
Password: c.BasicAuth.Password,
}
return tp.Client(), github.NewClient(tp.Client()), nil
}
}
return nil, nil, fmt.Errorf("No suitable credential found for %s", credentialName)
}
示例15: GetClient
func GetClient() *godo.Client {
token := &oauth2.Token{AccessToken: token}
t := oauth2.StaticTokenSource(token)
oauthClient := oauth2.NewClient(oauth2.NoContext, t)
return godo.NewClient(oauthClient)
}