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

     1  package linkedin
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  
     7  	"github.com/parnurzeal/gorequest"
     8  )
     9  
    10  const (
    11  	// DataEndpoint linkedin profile data endpoint url
    12  	DataEndpoint = "https://api.linkedin.com/v1/people/~:(id,first-name,last-name,email-address,picture-url,location,positions,headline)"
    13  )
    14  
    15  var tokenTypes = map[string]string{
    16  	"oauth":  "oauth_token",
    17  	"oauth2": "oauth2_access_token",
    18  }
    19  
    20  // User linkedin user data
    21  type User struct {
    22  	ID         string
    23  	FirstName  string `json:"firstName"`
    24  	LastName   string `json:"lastName"`
    25  	Email      string `json:"emailAddress"`
    26  	PictureURL string `json:"pictureUrl"`
    27  	Headline   string
    28  	Positions  UserPositions
    29  	Location   struct {
    30  		Country struct {
    31  			Code string
    32  		}
    33  		Name string
    34  	}
    35  	ErrorCode int `json:"errorCode"`
    36  	Message   string
    37  	Status    int
    38  }
    39  
    40  // UserPositions linkedin user postions data
    41  type UserPositions struct {
    42  	Values []UserPosition
    43  }
    44  
    45  // UserPosition linkedin user position data
    46  type UserPosition struct {
    47  	ID        int
    48  	Title     string
    49  	IsCurrent bool `json:"isCurrent"`
    50  	Company   Company
    51  	Location  struct {
    52  		Country struct {
    53  			Code string
    54  			Name string
    55  		}
    56  	}
    57  	StartDate struct {
    58  		Month int
    59  		Year  int
    60  	} `json:"startDate"`
    61  }
    62  
    63  // Company linkedin company data
    64  type Company struct {
    65  	ID       int
    66  	Name     string
    67  	Industry string
    68  	Size     string
    69  	Type     string
    70  }
    71  
    72  // UserInfo linkedin user info by the access token
    73  func UserInfo(tokenType string, accessToken string) (*User, error) {
    74  	user := new(User)
    75  
    76  	request := gorequest.New().Get(DataEndpoint)
    77  	_, body, errs := request.
    78  		Param(tokenTypes[tokenType], accessToken).
    79  		Param("format", "json").
    80  		End()
    81  	if errs != nil {
    82  		return user, errs[0]
    83  	}
    84  
    85  	if err := json.Unmarshal([]byte(body), &user); err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	if user.Message != "" {
    90  		return user, errors.New(user.Message)
    91  	}
    92  
    93  	return user, nil
    94  }