Gotosocial 如何获得Access Tokens

2087 次浏览
4 评论

共计3428个字符,预计需要花费 5分钟才能阅读完成。

AI摘要:本文介绍了如何通过Gotosocial API获得Access Tokens。首先需要使用API创建一个应用,得到"client_id"和"client_secret"。然后授权应用,访问指定的URL并点击通过,得到一个token。最后,使用获得的token和之前得到的"client_id"和"client_secret",发送请求获得access token。

使用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

正文完
 0
评论(4 条评论)
  1. 恐咖兵糖 初识 LV.2
    2024-05-28 15:11:00 回复

    可用开源工具参考
    https://takahashim.github.io/mastodon-access-token/

      Android   Chrome  黑龙江省哈尔滨市联通
    1. 老孙 博主
      2024-05-28 15:13:18 回复
      @恐咖兵糖

      是的,我这是按照官方文档来操作的. 纯属闲的没事

        Windows   Chrome  美国加利福尼亚
      1. 恐咖兵糖 初识 LV.2
        2024-05-28 15:24:50 回复
        @老孙

        还有个有趣的,闲着没事的玩法,payload json 加个 client_website 字段,获取有发布权限 token ,使用此 token 发布嘟嘟,在一些客户端嘟嘟显示页面,显示 client_name 和 client_website 外链。

        {

          "client_name": "SunBlog",
          "client_website": "https://www.imsun.org/"

        }

          Android   Chrome  黑龙江省哈尔滨市联通
        1. 老孙 博主
          2024-05-28 15:26:31 回复
          @恐咖兵糖

          哈哈哈,还是你玩得更深入些

            Windows   Chrome  美国加利福尼亚
联系我们

SiteMap| RSS|

版权说明

版权所有 转载请注明出处


© 2025 老孙博客
 Theme by Puock  Powered by Typecho

老孙博客制作