默认 366 4

    Gotosocial 如何获得Access Tokens

    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://bbapi.ima.cm

    老孙
    资深网民孙先生

    Notice: Undefined index: HTTP_ACCEPT_LANGUAGE in /www/wwwroot/imsun.org/usr/themes/typecho-theme-farallon/comments.php on line 4
    1. 恐咖兵糖
      2024-05-28 15:11
        1. 老孙
          2024-05-28 15:13
          @恐咖兵糖

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

            1. 恐咖兵糖
              2024-05-28 15:24
              @老孙

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

              {

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

              }

                1. 老孙
                  2024-05-28 15:26
                  @恐咖兵糖

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