Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

2 answers

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)

2 comments

0

no. it just shows http 401. code is in my answer.

0

401 means your request was unauthorised; I can't help you with that.

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.