Giter Club home page Giter Club logo

getreddittoken's Introduction

GetRedditToken

This tutorial is intended for Reddit developers who need to retrieve their API access token. I spent a significant amount of time trying to figure out how to do this, and the recommended guide did not help, as I kept getting various errors. I hope this tutorial will be more helpful to others.

An API access token is required to access the Reddit API. To retrieve your access token, you can use the following PowerShell script.

Setup

Your first step will need to be creating a Reddit app

  1. Go to https://www.reddit.com/prefs/apps once you've logged into Reddit.
  2. Click "Create App"
  3. Enter name, select "script", and enter everything. None of the following fields really matter since they won't be used. image
  4. Click "Create App"

PowerShell

Once you have an App ID and a secret key, you will need to save the following into a text document:

$Headers = @{
    'User-Agent'  = 'User agent'
    'Authorization' = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes('[App Id]:[Secret Key]'))
}

$Body = @{
    'grant_type' = 'password'
    'username'   = '[username]'
    'password'   = '[password]'
}

$response = Invoke-WebRequest -Uri 'https://www.reddit.com/api/v1/access_token' -Method POST -Headers $Headers -Body $Body

# Display the response content
$responseContent = $response.Content
Write-Host "Response Content:"
Write-Host $responseContent

In the above, replace anything in the brackets [] with the correct information. For example, where it says [App Id], then replace it your App ID from the above steps. Mine looks something like the following, except I scrambled some of the letters for security reasons:

$Headers = @{
    'User-Agent'  = 'User agent'
    'Authorization' = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes('pasFsdLQ3swX3dsfdzx35w:aeEGAROQdwqPftDD5UBCf_3rDGTnbs'))
}

$Body = @{
    'grant_type' = 'password'
    'username'   = 'lordkrehn'
    'password'   = 'l0ln1c3try'
}

$response = Invoke-WebRequest -Uri 'https://www.reddit.com/api/v1/access_token' -Method POST -Headers $Headers -Body $Body

# Display the response content
$responseContent = $response.Content
Write-Host "Response Content:"
Write-Host $responseContent

Save this text file as a PowerShell script (*.ps1) somewhere you can easily access it. I saved it on my desktop as "new.ps1".

Running the PowerShell

Now that you've created the script, you'll need to run it.

  1. Open PowerShell as an Administrator
  2. Type the following and hit enter
Set -ExecutionPolicy RemoteSigned
  1. Type "Y" and hit enter
  2. Navigate to your directory using the CD command. Mine was saved on the desktop
CD C:\Users\Me\Desktop
  1. Now, execute the script
\new.ps1
  1. If you set up everything correctly, you'll receive a response with your access token. It will be the long block of text following access_token:

image

getreddittoken's People

Contributors

rkrehn avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.