get access_token complete
This commit is contained in:
parent
750b3623b9
commit
b156a88254
|
@ -1,9 +1,13 @@
|
|||
package platform
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"up2GitX/share"
|
||||
|
||||
"github.com/gookit/gcli/v2"
|
||||
"github.com/gookit/color"
|
||||
"github.com/gookit/gcli/v2/interact"
|
||||
)
|
||||
|
||||
// options for the command
|
||||
|
@ -33,7 +37,49 @@ func syncGitee(c *gcli.Command, args []string) error {
|
|||
share.InvalidAlert(c.Name)
|
||||
} else {
|
||||
// todo nesOpts.forceSync
|
||||
share.ReadyToAuth(args[0])
|
||||
repos := share.ReadyToAuth(args[0])
|
||||
if repos != nil {
|
||||
askResult, success := askForAccount()
|
||||
if success {
|
||||
fmt.Println(askResult)
|
||||
} else {
|
||||
color.Red.Println(askResult)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func askForAccount() (string, bool) {
|
||||
email, _ := interact.ReadLine("Please enter your Gitee email: ")
|
||||
password := interact.ReadPassword("Please enter your Gitee password: ")
|
||||
if len(email) == 0 || len(password) == 0 {
|
||||
return "Email or Password must be provided!", false
|
||||
} else {
|
||||
params := fmt.Sprintf(`{
|
||||
"grant_type": "password",
|
||||
"username": "%s",
|
||||
"password": "%s",
|
||||
"client_id": "xxxxxxx",
|
||||
"client_secret": "xxxxxxxx",
|
||||
"scope": "projects groups enterprises"
|
||||
}`, email, password)
|
||||
|
||||
var paramsJson map[string]interface{}
|
||||
json.Unmarshal([]byte(params), ¶msJson)
|
||||
result, err := share.Post("https://gitee.com/oauth/token", paramsJson)
|
||||
|
||||
if err != nil {
|
||||
return err.Error(), false
|
||||
}
|
||||
|
||||
accessToken, atok := result["access_token"].(string)
|
||||
_, errok := result["error"].(string)
|
||||
if atok {
|
||||
return accessToken, true
|
||||
} else if errok {
|
||||
return result["error_description"].(string), false
|
||||
}
|
||||
}
|
||||
return "Unexpectedly exit", false
|
||||
}
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
package share
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
@ -106,8 +109,8 @@ func ReadyToAuth(repoDir string) []string {
|
|||
color.Red.Printf("No git repositories detected in %s \n", repoDir)
|
||||
} else {
|
||||
printRepos(repos)
|
||||
toGitee, _ := interact.ReadLine("Continue to auth Gitee? (y/n)")
|
||||
if toGitee == "y" {
|
||||
inPut, _ := interact.ReadLine("Continue to the next step? (y/n) ")
|
||||
if inPut == "y" {
|
||||
return repos
|
||||
} else {
|
||||
ExitMessage()
|
||||
|
@ -122,3 +125,38 @@ func ReadyToAuth(repoDir string) []string {
|
|||
func ExitMessage() {
|
||||
color.Yellow.Println("Bye, see you next time!")
|
||||
}
|
||||
|
||||
func Get(url string) (map[string]interface{}, error) {
|
||||
response, err := http.Get(url)
|
||||
if err != nil {
|
||||
color.Red.Printf("Request failed, Error: %s \n", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(response.Body)
|
||||
|
||||
var result map[string]interface{}
|
||||
json.Unmarshal(body, &result)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func Post(uri string, params map[string]interface{}) (map[string]interface{}, error) {
|
||||
data := url.Values{}
|
||||
for k, v := range params {
|
||||
data.Add(k, v.(string))
|
||||
}
|
||||
|
||||
response, err := http.PostForm(uri, data)
|
||||
if err != nil {
|
||||
color.Red.Printf("Request failed, Error: %s \n", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
body, _ := ioutil.ReadAll(response.Body)
|
||||
|
||||
var result map[string]interface{}
|
||||
json.Unmarshal(body, &result)
|
||||
return result, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue