Last active
November 27, 2024 11:42
-
-
Save adactio/1a850920a0554a49f36daf79d776a440 to your computer and use it in GitHub Desktop.
A PHP function that returns the token you need to post something to Bluesky
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Use this function whenever you need a token from Bluesky e.g. | |
$my_token = getBlueskyToken(); | |
*/ | |
function getBlueskyToken() { | |
$url = "https://bsky.social/xrpc/com.atproto.server.createSession"; | |
$payload = array( | |
"identifier" => 'YOUR-BLUESKY-HANDLE', | |
"password" => 'YOUR-APP-PASSWORD', | |
); | |
// Generate an app password at https://bsky.app/settings/app-passwords | |
$headers = array( | |
"Content-Type: application/json" | |
); | |
$options = array( | |
CURLOPT_URL => $url, | |
CURLOPT_USERAGENT => "adactio.com", | |
CURLOPT_RETURNTRANSFER => TRUE, | |
CURLOPT_POST => TRUE, | |
CURLOPT_HTTPHEADER => $headers, | |
CURLOPT_POSTFIELDS => json_encode($payload), | |
CURLOPT_TIMEOUT => 5 | |
); | |
$curl = curl_init(); | |
curl_setopt_array($curl, $options); | |
$response = curl_exec($curl); | |
curl_close($curl); | |
$data = json_decode($response, true); | |
return $data['accessJwt']; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment