mirror of
https://github.com/vwxyzjn/portwarden
synced 2025-12-29 05:13:14 +00:00
API-6 # Use Google API to get user info
This commit is contained in:
@@ -4,7 +4,7 @@ import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
|
||||
"github.com/vwxyzjn/portwarden/web/worker/server"
|
||||
"github.com/vwxyzjn/portwarden/web/scheduler/server"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -53,8 +53,8 @@ func (ps *PortwardenServer) GetGoogleDriveLoginHandler(c *gin.Context) {
|
||||
}
|
||||
spew.Dump(tok)
|
||||
GoogleDriveClient := ps.GoogleDriveAppConfig.Client(oauth2.NoContext, tok)
|
||||
fileBytes := []byte("xixix")
|
||||
err = UploadFile(fileBytes, GoogleDriveClient, tok)
|
||||
// fileBytes := []byte("xixix")
|
||||
err = GetUserInfo(GoogleDriveClient, tok)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "message": ErrRetrievingOauthCode})
|
||||
return
|
||||
|
||||
@@ -182,6 +182,53 @@ func UploadFile(fileBytes []byte, client *http.Client, token *oauth2.Token) erro
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetUserInfo(client *http.Client, token *oauth2.Token) error {
|
||||
|
||||
postURL := "https://www.googleapis.com/oauth2/v2/userinfo"
|
||||
|
||||
// Extract auth or access token from Token file
|
||||
// See https://godoc.org/gnolang.org/x/oauth2#Token
|
||||
authToken := token.AccessToken
|
||||
|
||||
// Post to Drive with RESTful method
|
||||
request, err := http.NewRequest("GET", postURL, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
request.Header.Add("Host", "www.googleapis.com")
|
||||
request.Header.Add("Authorization", "Bearer "+authToken)
|
||||
request.Header.Add("Content-Length", strconv.FormatInt(request.ContentLength, 10))
|
||||
|
||||
// For debugging
|
||||
//fmt.Println(request)
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer response.Body.Close()
|
||||
body, err := ioutil.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Output the response from Drive API
|
||||
fmt.Println(string(body))
|
||||
|
||||
// // Extract the uploaded file ID to execute further customization like update the file
|
||||
// jsonAPIreply, err := jason.NewObjectFromBytes(body)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
// uploadedFileID, err := jsonAPIreply.GetString("id")
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// fmt.Println("Uploaded file ID : ", uploadedFileID)
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
func main() {
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ type PortwardenServer struct {
|
||||
func (ps *PortwardenServer) Run() {
|
||||
var err error
|
||||
ps.GoogleDriveContext = context.Background()
|
||||
ps.GoogleDriveAppConfig, err = google.ConfigFromJSON(ps.GoogleDriveAppCredentials, drive.DriveScope)
|
||||
ps.GoogleDriveAppConfig, err = google.ConfigFromJSON(ps.GoogleDriveAppCredentials, "https://www.googleapis.com/auth/userinfo.profile", drive.DriveScope)
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to parse client secret file to config: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user