chore(scripts): convert meta id verifier to javascript
This commit is contained in:
+70
@@ -0,0 +1,70 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
const crypto = require("node:crypto");
|
||||||
|
|
||||||
|
const GRAPH_API_VERSION = "v25.0";
|
||||||
|
const PSID = "27511427698460020";
|
||||||
|
const EXPECTED_ASID = "122111097783118257";
|
||||||
|
const APP_ID = "26934819589512827";
|
||||||
|
const PAGE_ACCESS_TOKEN = "TODO_PAGE_ACCESS_TOKEN";
|
||||||
|
const APP_SECRET = "TODO_APP_SECRET";
|
||||||
|
|
||||||
|
function assertConfigured(name, value) {
|
||||||
|
if (!value || value.startsWith("TODO_")) {
|
||||||
|
throw new Error(`Please fill script constant: ${name}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function createAppSecretProof(accessToken, appSecret) {
|
||||||
|
return crypto
|
||||||
|
.createHmac("sha256", appSecret.trim())
|
||||||
|
.update(accessToken.trim())
|
||||||
|
.digest("hex");
|
||||||
|
}
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
assertConfigured("PAGE_ACCESS_TOKEN", PAGE_ACCESS_TOKEN);
|
||||||
|
assertConfigured("APP_SECRET", APP_SECRET);
|
||||||
|
|
||||||
|
const url = new URL(
|
||||||
|
`https://graph.facebook.com/${GRAPH_API_VERSION}/${PSID}/ids_for_apps`,
|
||||||
|
);
|
||||||
|
url.searchParams.set("app", APP_ID);
|
||||||
|
url.searchParams.set("access_token", PAGE_ACCESS_TOKEN);
|
||||||
|
url.searchParams.set(
|
||||||
|
"appsecret_proof",
|
||||||
|
createAppSecretProof(PAGE_ACCESS_TOKEN, APP_SECRET),
|
||||||
|
);
|
||||||
|
|
||||||
|
const response = await fetch(url);
|
||||||
|
const payload = await response.json();
|
||||||
|
|
||||||
|
console.log(JSON.stringify(payload));
|
||||||
|
|
||||||
|
if (payload.error) {
|
||||||
|
console.error(`Graph API error: ${payload.error.message ?? "unknown"}`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
console.error(`Graph API request failed with HTTP ${response.status}.`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const matched = Array.isArray(payload.data)
|
||||||
|
? payload.data.some((item) => item && String(item.id) === EXPECTED_ASID)
|
||||||
|
: false;
|
||||||
|
|
||||||
|
if (matched) {
|
||||||
|
console.log(`Matched: PSID maps to ASID ${EXPECTED_ASID}.`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Not matched: PSID does not map to ASID ${EXPECTED_ASID}.`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
main().catch((error) => {
|
||||||
|
console.error(error instanceof Error ? error.message : error);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
Executable
+62
@@ -0,0 +1,62 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
const GRAPH_API_VERSION = "v25.0";
|
||||||
|
const PSID = "27511427698460020";
|
||||||
|
const EXPECTED_ASID = "122111097783118257";
|
||||||
|
const APP_ID = "26934819589512827";
|
||||||
|
const PAGE_ACCESS_TOKEN = "TODO_PAGE_ACCESS_TOKEN";
|
||||||
|
|
||||||
|
function assertConfigured(name, value) {
|
||||||
|
if (!value || value.startsWith("TODO_")) {
|
||||||
|
throw new Error(`Please fill script constant: ${name}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
assertConfigured("PAGE_ACCESS_TOKEN", PAGE_ACCESS_TOKEN);
|
||||||
|
|
||||||
|
const url = new URL(
|
||||||
|
`https://graph.facebook.com/${GRAPH_API_VERSION}/${PSID}/ids_for_apps`,
|
||||||
|
);
|
||||||
|
url.searchParams.set("access_token", PAGE_ACCESS_TOKEN);
|
||||||
|
|
||||||
|
const response = await fetch(url);
|
||||||
|
const payload = await response.json();
|
||||||
|
|
||||||
|
console.log(JSON.stringify(payload));
|
||||||
|
|
||||||
|
if (payload.error) {
|
||||||
|
console.error(`Graph API error: ${payload.error.message ?? "unknown"}`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
console.error(`Graph API request failed with HTTP ${response.status}.`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const matched = Array.isArray(payload.data)
|
||||||
|
? payload.data.find((item) => String(item?.app?.id ?? "") === APP_ID)
|
||||||
|
: null;
|
||||||
|
|
||||||
|
if (!matched?.id) {
|
||||||
|
console.log(`Not found: no ASID record for app ${APP_ID}.`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const matchedAsid = String(matched.id);
|
||||||
|
console.log(`Found ASID for app ${APP_ID}: ${matchedAsid}`);
|
||||||
|
|
||||||
|
if (matchedAsid === EXPECTED_ASID) {
|
||||||
|
console.log(`Matched: PSID maps to expected ASID ${EXPECTED_ASID}.`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Not matched: expected ASID ${EXPECTED_ASID}, got ${matchedAsid}.`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
main().catch((error) => {
|
||||||
|
console.error(error instanceof Error ? error.message : error);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
set -euo pipefail
|
|
||||||
|
|
||||||
readonly GRAPH_API_VERSION="v25.0"
|
|
||||||
readonly PSID="27511427698460020"
|
|
||||||
readonly ASID="122111097783118257"
|
|
||||||
readonly APP_ID="26934819589512827"
|
|
||||||
readonly APP_SECRET="TODO_APP_SECRET"
|
|
||||||
readonly PAGE_ACCESS_TOKEN="TODO_PAGE_ACCESS_TOKEN"
|
|
||||||
|
|
||||||
appsecret_proof="$(
|
|
||||||
node -e '
|
|
||||||
const crypto = require("crypto");
|
|
||||||
const token = process.argv[1].trim();
|
|
||||||
const secret = process.argv[2].trim();
|
|
||||||
process.stdout.write(
|
|
||||||
crypto.createHmac("sha256", secret).update(token).digest("hex")
|
|
||||||
);
|
|
||||||
' "$PAGE_ACCESS_TOKEN" "$APP_SECRET"
|
|
||||||
)"
|
|
||||||
|
|
||||||
response="$(
|
|
||||||
curl -sS -G "https://graph.facebook.com/${GRAPH_API_VERSION}/${PSID}/ids_for_apps" \
|
|
||||||
--data-urlencode "app=${APP_ID}" \
|
|
||||||
--data-urlencode "access_token=${PAGE_ACCESS_TOKEN}" \
|
|
||||||
--data-urlencode "appsecret_proof=${appsecret_proof}"
|
|
||||||
)"
|
|
||||||
|
|
||||||
echo "$response"
|
|
||||||
|
|
||||||
if echo "$response" | node -e '
|
|
||||||
const fs = require("fs");
|
|
||||||
const expected = process.argv[1];
|
|
||||||
const payload = JSON.parse(fs.readFileSync(0, "utf8"));
|
|
||||||
const matched = Array.isArray(payload.data)
|
|
||||||
&& payload.data.some((item) => item && String(item.id) === expected);
|
|
||||||
process.exit(matched ? 0 : 1);
|
|
||||||
' "$ASID"; then
|
|
||||||
echo "Matched: PSID maps to ASID ${ASID}."
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Not matched: PSID does not map to ASID ${ASID}."
|
|
||||||
exit 1
|
|
||||||
Reference in New Issue
Block a user