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);
|
||||
});
|
||||
Reference in New Issue
Block a user