OAuth 2.0 with Powershell for Unsplash API bearer token

How to get a bearer token to invoke unsplash API calls using OAUTH and Powershell

Photo by Douglas Lopes on Unsplash

THE REQUIREMENT

Get a bearer token that never expires into an API using the OATH 2.0 process and Powershell.

THE SOLUTION

I’d like to have a bearer token that never expires once and then use that token forever to get information from the Unsplash API and my pictures statistics.

Create a new application for Unsplash

The whole idea is to go to the page: https://unsplash.com/oauth/applications, log in with your unsplash credentials, if you don’t have one, create a new account.

Redirect URI added into the Permissions

Run the script

With that information, you are enabled to run the script and get information:

.\Do-Oauth20forUnsplash.ps1
Authorization: Bearer ACCESS_TOKEN

Now only run a query for the API using Powershell

To get a random image, it’s not required to use the bearer token you use the ClientID instead (AccessKey):

Invoke-WebRequest -Method Get "https://api.unsplash.com/photos/random?client_id=pTkyrGk2d-eJnfSNrh1jVIU96kS7sOCRMf1ZYUK96nU"

Something using the bearer token /me:

Invoke-WebRequest -Method Get -Headers @{'Authorization'='Bearer hC7dNvBEVYyk_rV5DGfSR5L_Y7sTAu20qWF764GCZQU'} -Uri "https://api.unsplash.com/me"

And the conversion from the JSON into PS Object:

Invoke-WebRequest -Method Get -Headers @{'Authorization'='Bearer hC7dNvBEVYyk_rV5DGfSR5L_Y7sTAu20qWF764GCZQU'} -Uri "https://api.unsplash.com/me" | select -ExpandProperty Content | ConvertFrom-Json

Powershell | Office365 | Exchange Server/Online | Microsoft Certified Professional | Experts-Exchange Awarded 2018