无法使用气体从Reddit API获取访问令牌

发布于 2025-02-10 12:18:19 字数 2470 浏览 4 评论 0原文

我正在尝试使用获得的客户端ID和秘密 https://www.reddit.com/ prefs/apps

文档在这里: https://ads-api.reddit.com /docs/

应用程序名称是“报告”,这是“ Web应用程序”(选择脚本时我没有问题),

无论我尝试什么,我都会得到{“消息”:“不好的请求“,”错误”:400}

我正在使用Apts脚本/Google表格实现此功能,

我尝试了

Auth url:

https://www.reddit.com/api/v1/authorize?client_id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxtype_type = code = code = relect_uri = https_uri = 3SGO3NPGK01QIUSSANY43FFNAUGG2Q6B%2FUSERCALLBACK& amp; amp; = adepc8xyajuigafm1are-n3gjrw-olvi08gr23y1ouqjplpgtpgtz6zpwtcpqra8kw79abqkxha1b8d90tb8b8b8b8b8b4jjjjjjjjjjjckkkzkkkzkkzkzjcggwdxjcgwdxj8lpdudnfuup nbnfuup anbnbnbnbnbnbnbnbnbnbnbnbnbnbnbhpnpdyquud qurpdyyquddyqudyqudyqudyqudyqudyquddyyqudyqudyqudyqudyquddyquud qurpnpn Q.RPNPD.ERPNPD.RPNPNPNPRIUP P2ZIF4WTT-YAPR8VBBRKHR0TC6ASCEWNQPE9SCSOCCRK6DITZ2SWFP_ZNA5RD03UP-F2HRJFMEORMPFFLZVFLZVFLZVFLZDBZVGDBZVGGGGGGGGGGGGGGGGGIPEEPEEPEEPEEPEEPEEPEEPEEPEEPEEPEEPEEPEEPEEPEEPEEPEEPEEPEEPEEPEEPEEPEEPEEPEEPEEPEEPEEPEEPEEPEEPEEPEEPEE UF8M_MBWKHOHO-69G9RNKCHMH-CTO_HGNP8-53TN_VICSQM2XF2XF2DQ_W& scope = adsread%20 history& puration& permp; permp; permap;

Redirect:

https://script.google.com/macros/d/1Nh3AKooeG8XH4rSTvEWLTf5l3Sgo3nPgK01qiusSany43fFNaUgg2q6b/usercallback?state=ADEpC8xyajUigAfm1arE-N3gjrw-OLvI08gR23Y1ouQJpLpGtZ6ZPWTcpQRa8kW79ABqkxha1B8d90tb8B4eianNmJjFgzKzjCGwdxJ8lpDUdyNF7YjNq_3ak8d6Co_arPUDuP-BnbH0qRSwuEacP2Zif4wTt-YapR8VBbrkhr0tc6aScewnQPe9sCsOccRK6dITZ2SWFP_ZNA5rd03uP-f2HRJfMEoRMPFFLzvfYs9CvgDbzIopee4k_6jqiEg02zK_BuQSOijkUweB3oBTGpcQxHsU1QcRmXoXuzjucN8uF8M_MbWKhOhO-69g9rnKcHmH-Cto_hgnP8-53tn_ViCsqM2xf2dQ_w&code=m0PsSaLd1kKVZs7jymk1Qn69hXsTQw#_

Request

curl -X POST https://www.reddit.com/api/v1/access_token \

-H 'content-type: application/x-www-form-urlencoded' \

-A 'username' \

-u xxxxxxxxxxxxx:yyyyyyyyyyyyyyyyyyyy \

-d 'grant_type=authorization_code&code=m0PsSaLd1kKVZs7jymk1Qn69hXsTQw#_&redirect_uri=https%3A%2F%2Fscript.google.com%2Fmacros%2Fd%2F1Nh3AKooeG8XH4rSTvEWLTf5l3Sgo3nPgK01qiusSany43fFNaUgg2q6b%2Fusercallback'

Response

{"message": "Bad Request", "error": 400}

I am trying to fetch the access token using obtained client ID and secret https://www.reddit.com/prefs/apps

The documentation is here: https://ads-api.reddit.com/docs/

The app name is "Reporting", this is selected as "web app" (I have no issues when script is chosen)

No matter what I try, I am getting {"message": "Bad Request", "error": 400}

I am implementing this using Apps script/Google Sheets

I have tried with

Auth URL:

https://www.reddit.com/api/v1/authorize?client_id=xxxxxxxxxxxx&response_type=code&redirect_uri=https%3A%2F%2Fscript.google.com%2Fmacros%2Fd%2F1Nh3AKooeG8XH4rSTvEWLTf5l3Sgo3nPgK01qiusSany43fFNaUgg2q6b%2Fusercallback&state=ADEpC8xyajUigAfm1arE-N3gjrw-OLvI08gR23Y1ouQJpLpGtZ6ZPWTcpQRa8kW79ABqkxha1B8d90tb8B4eianNmJjFgzKzjCGwdxJ8lpDUdyNF7YjNq_3ak8d6Co_arPUDuP-BnbH0qRSwuEacP2Zif4wTt-YapR8VBbrkhr0tc6aScewnQPe9sCsOccRK6dITZ2SWFP_ZNA5rd03uP-f2HRJfMEoRMPFFLzvfYs9CvgDbzIopee4k_6jqiEg02zK_BuQSOijkUweB3oBTGpcQxHsU1QcRmXoXuzjucN8uF8M_MbWKhOhO-69g9rnKcHmH-Cto_hgnP8-53tn_ViCsqM2xf2dQ_w&scope=adsread%20history&duration=permanent

Redirect:

https://script.google.com/macros/d/1Nh3AKooeG8XH4rSTvEWLTf5l3Sgo3nPgK01qiusSany43fFNaUgg2q6b/usercallback?state=ADEpC8xyajUigAfm1arE-N3gjrw-OLvI08gR23Y1ouQJpLpGtZ6ZPWTcpQRa8kW79ABqkxha1B8d90tb8B4eianNmJjFgzKzjCGwdxJ8lpDUdyNF7YjNq_3ak8d6Co_arPUDuP-BnbH0qRSwuEacP2Zif4wTt-YapR8VBbrkhr0tc6aScewnQPe9sCsOccRK6dITZ2SWFP_ZNA5rd03uP-f2HRJfMEoRMPFFLzvfYs9CvgDbzIopee4k_6jqiEg02zK_BuQSOijkUweB3oBTGpcQxHsU1QcRmXoXuzjucN8uF8M_MbWKhOhO-69g9rnKcHmH-Cto_hgnP8-53tn_ViCsqM2xf2dQ_w&code=m0PsSaLd1kKVZs7jymk1Qn69hXsTQw#_

Request

curl -X POST https://www.reddit.com/api/v1/access_token \

-H 'content-type: application/x-www-form-urlencoded' \

-A 'username' \

-u xxxxxxxxxxxxx:yyyyyyyyyyyyyyyyyyyy \

-d 'grant_type=authorization_code&code=m0PsSaLd1kKVZs7jymk1Qn69hXsTQw#_&redirect_uri=https%3A%2F%2Fscript.google.com%2Fmacros%2Fd%2F1Nh3AKooeG8XH4rSTvEWLTf5l3Sgo3nPgK01qiusSany43fFNaUgg2q6b%2Fusercallback'

Response

{"message": "Bad Request", "error": 400}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落叶缤纷 2025-02-17 12:18:19

我相信您的目标如下。

  • 通过访问授权URL,例如https://www.reddit.com/api/v1/authorize?client_client_id=client_Id&Ampponseponse_tepe_type = code = code = code = code = code = code = random_state = random_state = random_state&am代码>使用浏览器,您需要使用Google Apps脚本创建的Web应用程序检索访问令牌。

在这种情况下,以下curl命令的请求由Google Apps脚本通过检索授权代码运行。

 curl -X POST https://www.reddit.com/api/v1/access_token \\
   -H 'content-type: application/x-www-form-urlencoded' \\
   -A 'CLIENT_NAME' \\
   -u CLIENT_ID:APP_SECRET \\
   -d 'grant_type=authorization_code&code=CODE&redirect_uri=REDIRECT_URL'

在这种情况下,以下Web应用程序的示例脚本如何?

示例脚本:

function doGet(e) {
  const clientId = "###"; // Please set your client ID.
  const secret = "###"; // Please set your secret.
  const redirectUrl = "https://script.google.com/macros/s/###/exec"; // Please set your Web Apps URL that you are using it as the redirect URL.

  const url = "https://www.reddit.com/api/v1/access_token";
  const params = {
    method: "post",
    headers: { "Authorization": "Basic " + Utilities.base64Encode(`${clientId}:${secret}`) },
    payload: {
      "code": e.parameter.code,
      "grant_type": "authorization_code",
      "redirect_uri": redirectUrl
    }
  };
  const res = UrlFetchApp.fetch(url, params);
  return ContentService.createTextOutput(res.getContentText());
}
  • 在脚本中设置变量后,请将修改后的脚本反映为Web应用程序。

  • 在此脚本中将 修改的脚本反映为Web应用程序。 .com/api/v1/authorize?client_id=xxxxxxxxxxxx&response_type=code&redirect_uri=https%3A%2F%2Fscript.google.com%2Fmacros%2Fd%2F1Nh3AKooeG8XH4rSTvEWLTf5l3Sgo3nPgK01qiusSany43fFNaUgg2q6b%2Fusercallback&state=ADEpC8xyajUigAfm1arE-N3gjrw-OLvI08gR23Y1ouQJpLpGtZ6ZPWTcpQRa8kW79ABqkxha1B8d90tb8B4eianNmJjFgzKzjCGwdxJ8lpDUdyNF7YjNq_3ak8d6Co_arPUDuP-BnbH0qRSwuEacP2Zif4wTt- YapR8VBbrkhr0tc6aScewnQPe9sCsOccRK6dITZ2SWFP_ZNA5rd03uP -f2hrjfmeormpfflzvfys9cvgdbziopee4k_6jqieg02zk_buqsoijkuweb3obtgpcqxhsu1qcrmxoxuzjucnuf8m_mbwkhoho-69 g9rnkchm_gnqmnkmchmhwqmhhw&and-24-thp4-thp4-thp4-thp4-thp4-thp4-thp4-thp4-tp ; scope = adsRead%20历史& lisation = enderent ,然后单击“箭头”按钮,授权代码已发送到您的Web应用程序,并且访问令牌和刷新令牌被检索为以下。

      {“ access_token”:“ ###”,“ token_type”:“ bearer”,“ expires_in”:86400,“ refresh_token”:“ ###”,“ scope”,“ scope”:“ adsread历史”}
     

注意:

  • 在此修改后的脚本中,它假设您的函数batcharchiveemail()正常工作。请小心。

  • 如果您禁用V8运行时,我认为可以在没有上述修改的情况下使用脚本。但是在这种情况下,循环的过程成本将变得很高。 ref ref 在此上

  • 在这种情况下,Web应用程序的设置必须为执行为:ME谁可以访问该应用程序:任何人 for New IDE。请小心。

I believe your goal is as follows.

  • By accessing the authorization URL like https://www.reddit.com/api/v1/authorize?client_id=CLIENT_ID&response_type=code&state=RANDOM_STRING&redirect_uri=REDIRECT_URL&duration=DURATION&scope=SCOPE_STRING with your browser, you want to retrieve the access token using Web Apps created by Google Apps Script.

In this case, the request of the following curl command is run by Google Apps Script by retrieving the authorization code.

 curl -X POST https://www.reddit.com/api/v1/access_token \\
   -H 'content-type: application/x-www-form-urlencoded' \\
   -A 'CLIENT_NAME' \\
   -u CLIENT_ID:APP_SECRET \\
   -d 'grant_type=authorization_code&code=CODE&redirect_uri=REDIRECT_URL'

In this case, how about the following sample script of Web Apps?

Sample script:

function doGet(e) {
  const clientId = "###"; // Please set your client ID.
  const secret = "###"; // Please set your secret.
  const redirectUrl = "https://script.google.com/macros/s/###/exec"; // Please set your Web Apps URL that you are using it as the redirect URL.

  const url = "https://www.reddit.com/api/v1/access_token";
  const params = {
    method: "post",
    headers: { "Authorization": "Basic " + Utilities.base64Encode(`${clientId}:${secret}`) },
    payload: {
      "code": e.parameter.code,
      "grant_type": "authorization_code",
      "redirect_uri": redirectUrl
    }
  };
  const res = UrlFetchApp.fetch(url, params);
  return ContentService.createTextOutput(res.getContentText());
}
  • After you set the variables in the script, please reflect the modified script to the Web Apps.

  • In this script, when you access your authorization URL of https://www.reddit.com/api/v1/authorize?client_id=xxxxxxxxxxxx&response_type=code&redirect_uri=https%3A%2F%2Fscript.google.com%2Fmacros%2Fd%2F1Nh3AKooeG8XH4rSTvEWLTf5l3Sgo3nPgK01qiusSany43fFNaUgg2q6b%2Fusercallback&state=ADEpC8xyajUigAfm1arE-N3gjrw-OLvI08gR23Y1ouQJpLpGtZ6ZPWTcpQRa8kW79ABqkxha1B8d90tb8B4eianNmJjFgzKzjCGwdxJ8lpDUdyNF7YjNq_3ak8d6Co_arPUDuP-BnbH0qRSwuEacP2Zif4wTt- YapR8VBbrkhr0tc6aScewnQPe9sCsOccRK6dITZ2SWFP_ZNA5rd03uP-f2HRJfMEoRMPFFLzvfYs9CvgDbzIopee4k_6jqiEg02zK_BuQSOijkUweB3oBTGpcQxHsU1QcRmXoXuzjucN8uF8M_MbWKhOhO-69g9rnKcHmH-Cto_hgnP8-53tn_ViCsqM2xf2dQ_w&scope=adsread%20history&duration=permanent and click "Arrow" button, the authorization code is sent to your Web Apps, and the access token and the refresh token are retrieved as follows.

      {"access_token": "###", "token_type": "bearer", "expires_in": 86400, "refresh_token": "###", "scope": "adsread history"}
    

Note:

  • In this modified script, it supposes that your function batchArchiveEmail() works fine. Please be careful about this.

  • If you disable the V8 runtime, I thought that the script might be worked without the above modification. But in that case, the process cost of the loop will become high. Ref By this, I would like to introduce this workaround.

  • In this case, the setting of Web Apps is required to be Execute as: Me and Who has access to the app: Anyone for new IDE. Please be careful about this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文