Skip to content

Conversation

sonatard
Copy link

#119

I'm writing tests for OAuth Authrization API.
Please wait for next weekend.

Sample Code

package main                                                                          

import (                                                                              
    "flag"                                                                            
    "fmt"                                                                             
    "github.com/google/go-github/github"                                              
    "os"                                                                              
)                                                                                     

import (                                                                              
    "net/http"                                                                        
)                                                                                     

type Transport struct {                                                               
    Username string                                                                   
    Password string                                                                   
}                                                                                     

func (t Transport) RoundTrip(req *http.Request) (*http.Response, error) {             
    req.SetBasicAuth(t.Username, t.Password)                                          
    return http.DefaultTransport.RoundTrip(req)                                       
}                                                                                     

func (t *Transport) Client() *http.Client {                                           
    return &http.Client{Transport: t}                                                 
}                                                                                     

const Version string = "0.0.1"                                                        

func main() {                                                                         
    var user *string = flag.String("user", "", "Github user name")                    
    var pass *string = flag.String("password", "", "Github password")                 

    flag.Parse()                                                                      

    if len(*user) == 0 || len(*pass) == 0 {                                           
        flag.Usage()                                                                  
        os.Exit(1)                                                                    
    }                                                                                 

    // Basic Auth                                                                     
    t := &Transport{                                                                  
        Username: *user,                                                              
        Password: *pass,                                                              
    }                                                                                 
    client := github.NewClient(t.Client())                                            

    // Get                                                                            
    opt := &github.ListOptions{PerPage: 100}                                          
    auths, _, err := client.OAuth.ListAuthorizations(opt)                             
    if err != nil {                                                                   
        fmt.Printf("Error : client.OAuth.ListAuthorizations %s\n", err.Error())       
        os.Exit(0)                                                                    
    }                                                                                 

    for _, auth := range auths {                                                      
        fmt.Printf("ID : %v\n", *auth.ID)                                             
        a, _, err := client.OAuth.GetSingleAuthorization(*auth.ID)                    
        fmt.Printf("ID : %v\n", *a.ID)                                                
        if err != nil {                                                               
            fmt.Printf("Error : GetSingleAuthorization %s\n", err.Error())            
            os.Exit(1)                                                                
        }                                                                             
    }                                                                                 
    auth_req := &github.AuthorizationRequest {                                   
        Scopes: &[]string{"public_repo"},                                        
        Note:   github.String("go-github tset"),                                 
    }                                                                            
    new_auth, _, err := client.OAuth.CreateAuthorization(auth_req)               
    if err != nil {                                                              
        fmt.Printf("Error : client.OAuth.CreateAuthorization %s\n", err.Error()) 
        os.Exit(1)                                                               
    }                                                                            
    fmt.Printf("ID : %v\n", *new_auth.ID)                                        
}                                                                             
go run gauth.go -user YOUR_USER -password YOUR_PASS

@sonatard
Copy link
Author

I finished tests.
Please review.

Please tell me what to do next, If there is a job.

@willnorris willnorris changed the title Support OAuth Authrization API. Support OAuth Authorization API. Jan 1, 2016
@willnorris
Copy link
Collaborator

So I'll be merging this in just a moment. Since it took so long for me to get to reviewing this, I just made a number of changes myself, rather than leave comments. Most were relatively minor stylistic things:

  • rename to OAuthService to AuthorizationsService
  • rename oauth.go file to authorizations.go
  • combined GetOrCreate and GetOrCreateFingerprint into single method and rename to GetOrCreateForApp to distinguish it from the Get method which is getting an authorization by ID rather than by application client ID.
  • removed RevokeAll method, as it's not actually documented in the GitHub API (maybe it used to be?)
  • updated several methods that don't actually return an Authorization (Delete, Revoke)
  • simplified values in tests (having long strings doesn't actually test anything more than a single character does)
  • renamed Update method to Edit for consistency with the rest of the library
  • several small changes like variable renames for consistency with the rest of the library

If it looks like I missed something or messed something up, certainly let me know.

@willnorris willnorris closed this in 68828ce Apr 5, 2016
willnorris pushed a commit that referenced this pull request Apr 5, 2016
@sonatard
Copy link
Author

sonatard commented Apr 6, 2016

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants