文章

Gotosocial 如何获得Access Tokens

Gotosocial 如何获得Access Tokens

使用API创建一个应用

1
2
3
4
5
6
7
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”

1
2
3
4
5
6
7
{
    "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,类似

1
NDEYYZBKOTQTYTCWYI0ZMZKYLWE5OTYTZDHKMTG2MDQ3YJA

获得access token

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

1
2
3
4
5
6
7
8
9
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

1
2
3
4
5
6
{
    "access_token": "MJRJYJMXZGMTMGNJMC0ZYZQ0LWIZYTITZTAZMTUZNDNKYMJ1",
    "created_at": 1716722670,
    "scope": "read",
    "token_type": "Bearer"
}

通过Cloudflare Workers获取json数据

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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条公开的未包括回复的内容

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

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

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

替换以上的API节点

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

本文由作者按照 CC BY 4.0 进行授权