使用API创建一个应用

curl -X POST 'https://your.instance.url/api/v1/apps' \
-H 'Content-Type:application/json' \
-d '{
"client_name": "memos",
"redirect_uris": "urn:ietf:wg:oauth:2.0:oob",
"scopes": "read"
}'

得到”client_id”和”client_secret”

{
"id": "01MAXW228JRT327ACDW2MVQCR6",
"name": "memos",
"redirect_uri": "urn:ietf:wg:oauth:2.0:oob",
"client_id": "01D9XG149GN5RTEWWQE5MDPA",
"client_secret": "8799e15b-5978-4367-ba51-fd171fbb4d"
}

授权应用

访问 https://your.instance.url/oauth/authorize?client_id=your_new_client_id-id&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code

点击通过

得到了一个code,类似

NDEYYZBKOTQTYTCWYI0ZMZKYLWE5OTYTZDHKMTG2MDQ3YJA

获得access token

用上面获取的"client_id":,"client_secret","code"执行

curl -X POST 'https://your.instance.url/oauth/token' \
-H 'Content-Type:application/json'
-d '{
"redirect_uri": "urn:ietf:wg:oauth:2.0:oob",
"client_id": "01D9XG149GN5RTEWWQE5MDPA",
"client_secret": "8799e15b-5978-4367-ba51-fd171fbb4d",
"grant_type": "authorization_code",
"code": "NDEYYZBKOTQTYTCWYI0ZMZKYLWE5OTYTZDHKMTG2MDQ3YJA"
}'

获取access_token

{
"access_token": "MJRJYJMXZGMTMGNJMC0ZYZQ0LWIZYTITZTAZMTUZNDNKYMJ1",
"created_at": 1716722670,
"scope": "read",
"token_type": "Bearer"
}

通过Cloudflare Workers获取json数据

替换以下的{url} {id} {token}即可

addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const url = "https://{url}/api/v1/accounts/{id}/statuses?limit=1000&exclude_replies=true&only_public=true&only_media=true";
const init = {
method: "GET",
headers: {
"content-type": "application/json;charset=UTF-8",
"User-Agent": "Node.js/v14.15.1",
"Authorization": "Bearer {token}" // Use your real token here
}
};
let response = await fetch(url, init);
const results = await response.json();
// 构建新的响应并添加CORS头
let corsHeaders = {
"Access-Control-Allow-Origin": "*", // 这将允许所有源访问,如果你想限制访问可以更改为特定的URI
"Access-Control-Allow-Methods": "GET,POST,PUT,PATCH,DELETE,OPTIONS", // 你可以根据实际需要更改这些方法
"Access-Control-Allow-Headers": "Content-Type, Authorization, User-Agent" // 加入你用到的其他头
}
let newResponse = new Response(JSON.stringify(results), {
headers: corsHeaders
});
return newResponse;
}

根据API文档,此查询会默认获取30条 包括 回复 未公开的全部内容

获取最近1000条公开的未包括回复的内容

/api/v1/accounts/01MQ6Y9ZKC7TAJ7B97Q2TAMHXQ/statuses?limit=1000&exclude_replies=true&only_public=true

获取最近1000条公开的未回复的仅多媒体的内容

/api/v1/accounts/01MQ6Y9ZKC7TAJ7B97Q2TAMHXQ/statuses?limit=1000&exclude_replies=true&only_public=true&only_media=true

替换以上的API节点

演示地址 https://1000.ima.cm

本文作者:浪子 @ 提剑追梦

本文链接:https://blog.imsun.org/blog/1643/

本文标题:Gotosocial 如何获得Access Tokens

本文版权:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!