This a HTML app which allows OAuth2 authentication for LinkedIn developer API.
1.2K
This creates a local OAuth2 callback so you complete a 3-leg authorization flow. Resulting in an active session token which can be used for use with LinkedIn's developer API.
If you wish to run the application manually, you will need install golang and the associated oauth2 package:
go get golang.org/x/oauth2
If you wish to run the application though a container, you will need docker installed.
Before running the application, you will need to have a LinkedIn user account, enable your LinkedIn developer profile, and create/register an application.
Once the LinkedIn app is created, you will need to
Finally, it is recommended that you do NOT use the default "state" value. Instead, create a random "state" value. In theory, this will make the final session token more "secure'.
This is essentially, server authentication which requires no interation with an user. However, unless you pay for LinkedIn enterprise, you won't be able to use this option.
This is where the user authenticates themselves and then allows the application access to the LinkedIn resources. This application assists in this process by creating a callback url which can be redirected to in order to finalize the flow.
More information can be found here:
Here are the steps (high level overview):
Once you have a valid Access Token, you can start using LinkedIn API commands. A simple example would be to get your profile details. If it works, it should look like this:
$ curl -X GET 'https://api.linkedin.com/v2/me' \
-H 'Authorization: Bearer {access_token}'
{"localizedLastName":"Smith","profilePicture":{"displayImage":"urn:li:digitalmediaAsset:AAAAABBBBCCCC"},"firstName":{"localized":{"en_US":"John"},"preferredLocale":{"country":"US","language":"en"}},"lastName":{"localized":{"en_US":"Smith"},"preferredLocale":{"country":"US","language":"en"}},"id":"id1234","localizedFirstName":"John"}
You must 1st set the local ENV variables before you run the app. The default values are:
## Set the clientID and clientSecret
$env:LI_CALLBACK_URL = 'http://localhost:12345/oauth2/callback'
$env:LI_CLIENT_ID = 'id123'
$env:LI_CLIENT_SECRET = 'secret1234'
$env:LI_CLIENT_STATE = 'state12345'
$env:LI_CLIENT_SCOPE = 'r_liteprofile r_emailaddress w_member_social'
## Show the variables value
Get-ChildItem Env:LI_CALLBACK_URL
Get-ChildItem Env:LI_CLIENT_ID
Get-ChildItem Env:LI_CLIENT_SECRET
Get-ChildItem Env:LI_CLIENT_STATE
Get-ChildItem Env:LI_CLIENT_SCOPE
## Remove Item
Remove-Item Env:LI_CALLBACK_URL
Remove-Item Env:LI_CLIENT_ID
Remove-Item Env:LI_CLIENT_SECRET
Remove-Item Env:LI_CLIENT_STATE
Remove-Item Env:LI_CLIENT_SCOPE
## Set the clientID and clientSecret
export LI_CALLBACK_URL='http://localhost:12345/oauth2/callback'
export LI_CLIENT_ID='id123'
export LI_CLIENT_SECRET='secret1234'
export LI_CLIENT_STATE='state12345'
export LI_CLIENT_SCOPE='r_liteprofile r_emailaddress w_member_social'
## Show the variables value
echo $LI_CALLBACK_URL
echo $LI_CLIENT_ID
echo $LI_CLIENT_SECRET
echo $LI_CLIENT_STATE
echo $LI_CLIENT_SCOPE
## Remove ENV variables
unset LI_CALLBACK_URL
unset LI_CLIENT_ID
unset LI_CLIENT_SECRET
unset LI_CLIENT_STATE
unset LI_CLIENT_SCOPE
Once the variables are set, you can execute the app:
$ go run main.go
oauth2_callback_linkedin v2104.09a
www.2BitProgrammers.com
Copyright (C) 2020. All Rights Reserved.
LI Client Callback URL: http://localhost:12345/oauth2/callback
LI Client ID: id123
LI Client Secret: secret1234
LI Client State: state12345
LI Client Scope: r_liteprofile r_emailaddress w_member_social
2021/04/12 13:18:34 Starting App on Port 12345
2021/04/12 13:18:34 Login URL: https://www.linkedin.com/oauth/v2/authorization?client_id=id123&redirect_uri=http%3A%2F%2Flocalhost%3A12345%2Foauth2%2Fcallback&response_type=code&scope=r_liteprofile+r_emailaddress+w_member_social&state=state12345
CTRL+C
exit status 3221225786
$ docker build . -t 2bitprogrammers/oauth2_callback_linkedin
[+] Building 0.3s (15/15) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 1.18kB 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/golang:alpine 0.0s
=> [builder 1/8] FROM docker.io/library/golang:alpine 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 150B 0.0s
=> CACHED [builder 2/8] RUN apk --no-cache add ca-certificates 0.0s
=> CACHED [builder 3/8] WORKDIR /build 0.0s
=> CACHED [builder 4/8] COPY /go.mod . 0.0s
=> CACHED [builder 5/8] RUN go mod download 0.0s
=> CACHED [builder 6/8] COPY /main.go . 0.0s
=> CACHED [builder 7/8] COPY /html/ ./html/ 0.0s
=> CACHED [builder 8/8] RUN go build -o oauth2_callback_linkedin . 0.0s
=> [stage-1 1/3] COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ 0.0s
=> [stage-1 2/3] COPY --from=builder /build/oauth2_callback_linkedin /oauth2_callback_linkedin 0.1s
=> exporting to image 0.1s
=> => exporting layers 0.1s
=> => writing image sha256:be3a7f237761e871180d27590b5db1777f105b1539ae68925354a1360b67a622 0.0s
=> => naming to docker.io/2bitprogrammers/oauth2_callback_linkedin 0.0s
$ docker image ls 2bitprogrammers/oauth2_callback_linkedin
REPOSITORY TAG IMAGE ID CREATED SIZE
2bitprogrammers/oauth2_callback_linkedin latest fd18dbe46fe9 1 minutes ago 7.48MB
$ docker run --rm -d \
--name oauth2_callback_linkedin \
-e LI_CALLBACK_URL="http://localhost:12345/oauth2/callback" \
-e LI_CLIENT_ID="id123" \
-e LI_CLIENT_SECRET="secret1234" \
-e LI_CLIENT_STATE="state12345" \
-e LI_CLIENT_SCOPE="r_liteprofile r_emailaddress w_member_social" \
-p 12345:12345 \
2bitprogrammers/oauth2_callback_linkedin
## Verify that it is running
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
546be3de23be 2bitprogrammers/oauth2_callback_linkedin "/oauth2_callback_li…" 31 seconds ago Up 10 seconds 0.0.0.0:12345->12345/tcp oauth2_callback_linkedin
## Stopping the container
$ docker stop oauth2_callback_linkedin
## Verify that it is stopped
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Content type
Image
Digest
Size
3.8 MB
Last updated
over 5 years ago
docker pull 2bitprogrammers/oauth2_callback_linkedin