golinkedin

package module
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2024 License: MIT Imports: 6 Imported by: 0

README

Go LinkedIn

This is a Go client library for the LinkedIn API.

Installation

go get github.com/franciscoescher/golinkedin

Usage

Fiest, you need to create a client builder with your api key credentials using golinkedin.NewBuilder.

Then, you need to get the auth url to redirect the user to the linkedin login page using builder.GetAuthURL.

The user needs to access the url, login and accept the permissions. After that, linkedin will redirect to the redirect url with a code parameter.

Finally, You need to use this code to authenticate and get the client using builder.GetClient.

This client will be able to call the linkedin api.

package main

import (
	"context"
  "fmt"

  "github.com/franciscoescher/golinkedin"
)

func main() {
  // first, you need to create a builder with your api key credentials
  scopes := []string{"r_liteprofile", "r_emailaddress"}
  builder := golinkedin.NewBuilder("CLIENT_ID", "CLIENT_SECRET", scopes, "REDIRECT_URL")

  // then, you need to get the auth url to redirect the user to the linkedin login page
  url := builder.GetAuthURL("state")
  fmt.Println(url)

  // after user accepts, linkedin will redirect to REDIRECT_URL with a code parameter
  // you need to use this code to authenticate and get the client
  code := "CODE"
  client, err := builder.GetClient(context.Background(), code)
  if err != nil {
    log.Fatal(err)
  }

  // now you can use the client to make requests
  profile, err := client.GetProfile()
  if err != nil {
    log.Fatal(err)
  }
  fmt.Println(profile)
}

Please, check the example folder for a working example that implements a server that handles the linkedin callback.

Note

This library is not complete. It only implements the endpoints that I needed.

People endpoint is not tested.

Feel free to contribute.

Documentation

Index

Constants

View Source
const EndpointEmailAddress = "https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))"

EndpointEmailAddress is the endpoint for email address api.

View Source
const EndpointPrimaryContact = "https://api.linkedin.com/v2/clientAwareMemberHandles?q=members&projection=(elements*(primary,type,handle~))"

EndpointPrimaryContact is the endpoint for primary contact api.

View Source
const EndpointProfile = "" /* 206-byte string literal not displayed */

EndpointProfile is the endpoint for profile api.

Variables

View Source
var EndpointPeopleListParts = []string{"https://api.linkedin.com/rest/people?ids=List(", ")"}

EndpointPeopleListParts are the parts to build the endpoint for people list api.

View Source
var EndpointPeopleParts = []string{"https://api.linkedin.com/rest/people/(id:", ")"}

EndpointPeopleParts are the parts to build the endpoint for people api.

Functions

This section is empty.

Types

type BuilderInterface added in v1.3.0

type BuilderInterface interface {
	GetAuthURL(state string) string
	GetClient(ctx context.Context, code string) (ClientInterface, error)
}

builderInterface is the interface that wraps the basic GetAuthURL and GetClient methods.

func NewBuilder

func NewBuilder(clientID string, clientSecret string, scopes []string, redirectURL string) BuilderInterface

Newbuilder returns a new linkedin client, not yet authenticated.

type ClientInterface added in v1.3.0

type ClientInterface interface {
	Headers() map[string]string
	SetHeader(key string, value string)
	UnsetHeader(key string)
	Get(url string) (resp *http.Response, err error)
	PrimaryContactRequest() (resp *http.Response, err error)
	GetPrimaryContact() (r PrimaryContact, err error)
	GetEmailAddress() (r EmailAddress, err error)
	EmailAddressRequest() (resp *http.Response, err error)
	PeopleRequest(personID string) (resp *http.Response, err error)
	GetPeople(personID string) (r string, err error)
	PeopleListRequest(personIDs []string) (resp *http.Response, err error)
	GetPeopleList(personIDs []string) (r string, err error)
	ProfileRequest() (resp *http.Response, err error)
	GetProfile() (r Profile, err error)
}

func NewClient added in v1.3.0

func NewClient(httpClient http.Client, initialHeaders map[string]string) ClientInterface

type EmailAddress

type EmailAddress struct {
	ErrorResponse
	Elements []struct {
		Handle      string `json:"handle"`
		HandleTilde struct {
			EmailAddress string `json:"emailAddress"`
		} `json:"handle~"`
	} `json:"elements"`
}

EmailAddress is the response from email address api.

type ErrorResponse

type ErrorResponse struct {
	ServiceErrorCode int    `json:"serviceErrorCode"`
	Message          string `json:"message"`
	Status           int    `json:"status"`
}

Common struct for error response in linkedin API

type PrimaryContact

type PrimaryContact struct {
	ErrorResponse
	Elements []struct {
		Handle      string `json:"handle"`
		HandleTilde struct {
			EmailAddress string `json:"emailAddress"`
			PhoneNumber  struct {
				Number string `json:"number"`
			} `json:"phoneNumber"`
		} `json:"handle~"`
		Primary bool   `json:"primary"`
		Type    string `json:"type"`
	} `json:"elements"`
}

PrimaryContact is the response from primary contact api.

type Profile

type Profile struct {
	ErrorResponse
	ID                 string `json:"id"`
	LocalizedFirstName string `json:"localizedFirstName"`
	LocalizedLastName  string `json:"localizedLastName"`
	FirstName          struct {
		Localized struct {
			EnUS string `json:"en_US"`
		} `json:"localized"`
		PreferredLocale struct {
			Country  string `json:"country"`
			Language string `json:"language"`
		} `json:"preferredLocale"`
	} `json:"firstName"`
	LastName struct {
		Localized struct {
			EnUS string `json:"en_US"`
		} `json:"localized"`
		PreferredLocale struct {
			Country  string `json:"country"`
			Language string `json:"language"`
		} `json:"preferredLocale"`
	} `json:"lastName"`
	ProfilePicture struct {
		DisplayImage     string `json:"displayImage"`
		DisplayImageFull struct {
			Paging struct {
				Count int   `json:"count"`
				Start int   `json:"start"`
				Links []any `json:"links"`
			} `json:"paging"`
			Elements []struct {
				Artifact            string `json:"artifact"`
				AuthorizationMethod string `json:"authorizationMethod"`
				Data                struct {
					ComLinkedinDigitalmediaMediaartifactStillImage struct {
						MediaType    string `json:"mediaType"`
						RawCodecSpec struct {
							Name string `json:"name"`
							Type string `json:"type"`
						} `json:"rawCodecSpec"`
						DisplaySize struct {
							Width  float64 `json:"width"`
							Uom    string  `json:"uom"`
							Height float64 `json:"height"`
						} `json:"displaySize"`
						StorageSize struct {
							Width  int `json:"width"`
							Height int `json:"height"`
						} `json:"storageSize"`
						StorageAspectRatio struct {
							WidthAspect  float64 `json:"widthAspect"`
							HeightAspect float64 `json:"heightAspect"`
							Formatted    string  `json:"formatted"`
						} `json:"storageAspectRatio"`
						DisplayAspectRatio struct {
							WidthAspect  float64 `json:"widthAspect"`
							HeightAspect float64 `json:"heightAspect"`
							Formatted    string  `json:"formatted"`
						} `json:"displayAspectRatio"`
					} `json:"com.linkedin.digitalmedia.mediaartifact.StillImage"`
				} `json:"data"`
				Identifiers []struct {
					Identifier                 string `json:"identifier"`
					Index                      int    `json:"index"`
					MediaType                  string `json:"mediaType"`
					File                       string `json:"file"`
					IdentifierType             string `json:"identifierType"`
					IdentifierExpiresInSeconds int    `json:"identifierExpiresInSeconds"`
				} `json:"identifiers"`
			} `json:"elements"`
		} `json:"displayImage~"`
	} `json:"profilePicture"`
	Headline struct {
		Localized struct {
			EnUS string `json:"en_US"`
		} `json:"localized"`
		PreferredLocale struct {
			Country  string `json:"country"`
			Language string `json:"language"`
		} `json:"preferredLocale"`
	} `json:"headline"`
	LocalizedHeadline string `json:"localizedHeadline"`
	VanityName        string `json:"vanityName"`
}

Profile is the response from profile api.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL