github.com/seeker-insurance/kit@v0.0.13/oauth/facebook/facebook.go (about)

     1  package facebook
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"github.com/parnurzeal/gorequest"
     7  )
     8  
     9  const (
    10  	Fields        = "first_name, last_name, email, timezone, gender, picture.height(800)"
    11  	GraphEndpoint = "https://graph.facebook.com/me"
    12  )
    13  
    14  type GraphInfo struct {
    15  	Error     graphError
    16  	Id        string
    17  	FirstName string `json:"first_name"`
    18  	LastName  string `json:"last_name"`
    19  	Name      string
    20  	Picture   picture
    21  	Email     string
    22  	Timezone  int
    23  	Gender    string
    24  }
    25  
    26  type graphError struct {
    27  	Message   string
    28  	Type      string
    29  	Code      int
    30  	FbTraceId string `json:"fbtrace_id"`
    31  }
    32  
    33  type picture struct {
    34  	Data struct {
    35  		Url string
    36  	}
    37  }
    38  
    39  func FbUserInfo(accessToken string) (GraphInfo, error) {
    40  	var gInfo GraphInfo
    41  
    42  	request := gorequest.New().Get(GraphEndpoint)
    43  
    44  	_, body, errs := request.Param("access_token", accessToken).Param("fields", Fields).End()
    45  
    46  	if errs != nil {
    47  		return gInfo, errs[0]
    48  	}
    49  
    50  	err := json.Unmarshal([]byte(body), &gInfo)
    51  	if gInfo.Error.Message != "" {
    52  		return gInfo, errors.New(gInfo.Error.Message)
    53  	}
    54  	return gInfo, err
    55  }