Ad
Log in to vote
0

(I am fiukthemetaverse, on a old acc of mine) reddit api help?

Asked by 15 days ago

I was attempting to get a access token which references from this: https://alpscode.com/blog/how-to-use-reddit-api/ How do I make HTTPBasicAuth on lua? Trying to make the player comment on a reddit post (r/bloxit)

2 answers

Log in to vote
0
Answered by 15 days ago

Hi!

HTTPBasicAuth for the python requests library basically gives you a string that looks something like this: 'Basic VVNFUk5BTUU6UEFTU1dPUkQ='

To create something like this, you'd need to get a function that turns a string into something Base64 compatible (like this code, from the DevForum)

I've made a module script that should do the trick.

local LuaBasicAuth = {}

-- @xDeltaXen on DevForum
function to_base64(data: any): string
    local b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
    return ((data:gsub('.', function(x) 
        local r,b='',x:byte()
        for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
        return r;
    end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
        if (#x < 6) then return '' end
        local c=0
        for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
        return b:sub(c+1,c+1)
    end)..({ '', '==', '=' })[#data%3+1])
end

function LuaBasicAuth:GenerateAuth(username, password)
    local preencode = string.format('%s:%s', username, password)
    local encodedstring = string.format('Basic %s', to_base64(preencode))
    return encodedstring
end

return LuaBasicAuth

To use this module script:

local HTTPBasicAuth = require(AuthHeaderGeneration) -- replace AuthHeaderGeneration with the module script

local Username, Password = 'USERNAME', 'PASSWORD'

local AuthString = HTTPBasicAuth:GenerateAuth(Username, Password)

0
no. it just shows http 401. code is in my answer. TheScaryLogoFan500 0 — 15d
0
401 means your request was unauthorised; I can't help you with that. loowa_yawn 281 — 15d
Ad
Log in to vote
0
Answered by 15 days ago

Nope, but nice try, It just triggers a 401. code:

local http = game:GetService('HttpService')
local HTTPBasicAuth = require(game.Workspace.ty_scriptinghelpers) -- replace AuthHeaderGeneration with the module script
local base_url = "https://reddit.com/"
local data =  "{'grant_type': 'password', 'username': 'no', 'password': 'no"
local app_id, app_secret = "no", "no"
local auth = HTTPBasicAuth:GenerateAuth(app_id,app_secret)

local a = {
    ['data'] = data,
    ['headers'] = {
        ['user-agent'] = 'roblox-player-importing by blox-reddit-test'
    },
    ['auth'] = auth
}

local code = http:JSONEncode(a)

local r = http:PostAsync(base_url..'api/v1/access_token', code)

Answer this question