2017-04-18 1 views
0

oauth2로 google api 인증으로 로그인을 만들려고합니다. 내가 구글의 API (response.body)에서 응답을 받고 있어요 :Google api의 응답으로받은 데이터를 어떻게 사용할 수 있습니까?

{ 
"id": "received ID", 
"email": "EMAIL", 
"verified_email": true, 
"name": "Name", 
} 

어떻게 이동 프로그램 내부 데이터 나 데이터베이스에 저장할 수 있도록하는 것이 액세스 할 수 있습니까?

package main 

import (
    "fmt" 
    "io/ioutil" 
    "log" 
    "net/http" 
    "net/url" 
    "strings" 

    "golang.org/x/oauth2" 
    "golang.org/x/oauth2/google" 
    "encoding/json" 
) 

var (
    oauthConf = &oauth2.Config{ 
     ClientID:  "CLIENTID", 
     ClientSecret: "Secret", 
     RedirectURL: "http://localhost:8011/showprofile", 
     //Scopes:  []string{"https://www.googleapis.com/auth/plus.login"}, 
     Scopes:[]string{"profile","email"}, 
     Endpoint:  google.Endpoint, 
    } 
    oauthStateString = "thisshouldberandom" 
) 

const htmlIndex = `<html><body> 
Logged in with <a href="/login">facebook</a> 
</body></html> 
` 

func handleMain(w http.ResponseWriter, r *http.Request) { 
    w.Header().Set("Content-Type", "text/html; charset=utf-8") 
    w.WriteHeader(http.StatusOK) 
    w.Write([]byte(htmlIndex)) 
} 

func handleGoogleLogin(w http.ResponseWriter, r *http.Request) { 
    Url, err := url.Parse(oauthConf.Endpoint.AuthURL) 
    if err != nil { 
     log.Fatal("Parse: ", err) 
    } 
    parameters := url.Values{} 
    parameters.Add("client_id", oauthConf.ClientID) 
    parameters.Add("scope", strings.Join(oauthConf.Scopes, " ")) 
    parameters.Add("redirect_uri", oauthConf.RedirectURL) 
    parameters.Add("response_type", "code") 
    parameters.Add("state", oauthStateString) 
    Url.RawQuery = parameters.Encode() 
    url := Url.String() 
    fmt.Println("URL" + url) 
    http.Redirect(w, r, url, http.StatusTemporaryRedirect) 
} 

func handleGoogleCallback(w http.ResponseWriter, r *http.Request) { 
    fmt.Println("Call back working") 
    state := r.FormValue("state") 
    if state != oauthStateString { 
     fmt.Printf("invalid oauth state, expected '%s', got '%s'\n", oauthStateString, state) 
     http.Redirect(w, r, "/", http.StatusTemporaryRedirect) 
     return 
    } 

    code := r.FormValue("code") 

    token, err := oauthConf.Exchange(oauth2.NoContext, code) 
    if err != nil { 
     fmt.Printf("oauthConf.Exchange() failed with '%s'\n", err) 
     http.Redirect(w, r, "/", http.StatusTemporaryRedirect) 
     return 
    } 


    resp,err:= http.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + token.AccessToken) 

    if err != nil { 
     fmt.Printf("Get: %s\n", err) 
     http.Redirect(w, r, "/", http.StatusTemporaryRedirect) 
     return 
    } 
    fmt.Println(resp.Body) 
    defer resp.Body.Close() 

    response, err := ioutil.ReadAll(resp.Body) 
    if err != nil { 
     fmt.Printf("ReadAll: %s\n", err) 
     http.Redirect(w, r, "/showprofile", http.StatusTemporaryRedirect) 
     return 
    } 



    log.Printf("parseResponseBody: %s\n", string(response)) 

    http.Redirect(w, r, "/showprofile", http.StatusTemporaryRedirect) 
} 

func main() { 
    http.HandleFunc("/", handleMain) 
    http.HandleFunc("/login", handleGoogleLogin) 
    http.HandleFunc("/showprofile", handleGoogleCallback) 
    fmt.Print("Started running on http://localhost:8011\n") 
    log.Fatal(http.ListenAndServe(":8011", nil)) 
} 

답변

1

json.Unmarshal을 사용하여 해결했습니다.

관련 문제