- 您想使用Stackexchange API在Stackoverflow上检索自己的未读评论和声誉。
- 您要将检索到的值放入电子表格。
- 您想使用Google Apps脚本实现这一目标。
如果我的理解正确,那么这个答案如何?请认为这只是几个答案之一。
用法:
1。检索访问令牌
为了检索未读的评论和信誉,需要使用访问令牌。
流:
- Register an application at stackapps.com.
- 输入“应用程序名称”,“描述”,“ OAuth域”和“应用程序网站”。
- 作为示例凭证,我将它们输入为“ sampleApp”,“这是示例应用程序”,“ localhost” 和“ sample”。
- 要用作重定向uri,请将“ localhost” 设置为“ OAuth域”。
- 复制“客户ID”,“客户机密”和“密钥”。使用API还需要“密钥”。
- 授权范围。
- 请创建以下URL并使用浏览器进行访问。请设置您的client_id。
-
https://stackexchange.com/oauth?client_id=#####&scope=no_expiry%20read_inbox&redirect_uri=http://localhost
- 在此脚本中,“ no_expiry”和“ read_inbox”用作范围。这些范围用于检索未读的注释。
- 通过浏览器打开创建的URL时,请单击“批准”,如下所示。
- 从类似
### code ###
的浏览器URL复制代码http://localhost/?code=### code ###
。
-
使用“客户端ID”,“客户端密码”和“代码”检索访问令牌。请运行以下curl命令。这样,您可以检索访问令牌,例如access_token=#####
。
-
此访问令牌没有到期时间,因为范围中包含“ no_expiry”。因此,您可以继续使用此访问令牌。
curl \
-d "client_id=#####" \
-d "client_secret=#####" \
-d "code=#####" \
-d "redirect_uri=http://localhost" \
"https://stackexchange.com/oauth/access_token"
2。运行脚本
示例脚本1:
此示例脚本检索未读的注释。
function myFunction() {
var key = "###"; // Please set your key.
var accessToken = "###"; // Please set your access token.
var spreadsheetId = "###"; // Please set the Spreadsheet ID.
var url = "https://api.stackexchange.com/2.2/inbox/unread?filter=withbody&pagesize=100&access_token=" + accessToken + "&key=" + key;
var comments = JSON.parse(UrlFetchApp.fetch(url).getContentText());
var headers = ["creation_date","link","title","body"];
var values = comments.items.map(function(item) {return headers.map(function(h) {return item[h]})});
values.unshift(headers);
var sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName("Sheet1");
sheet.getRange(1,1,values.length,values[0].length).setValues(values);
}
示例脚本2:
此示例脚本检索信誉历史记录。
function myFunction() {
var key = "###"; // Please set your key.
var accessToken = "###"; // Please set your access token.
var userId = "###"; // Please set your user ID on Stackoverflow.
var spreadsheetId = "###"; // Please set the Spreadsheet ID.
var url = "https://api.stackexchange.com/2.2/users/" + userId + "/reputation-history/full?site=stackoverflow&pagesize=100&access_token=" + accessToken + "&key=" + key;
var reputation = JSON.parse(UrlFetchApp.fetch(url).getContentText());
var headers = ["creation_date","reputation_change","post_id"];
var values = reputation.items.map(function(item) {return headers.map(function(h) {return item[h]})});
values.unshift(headers);
var sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName("Sheet1");
sheet.getRange(1,values[0].length).setValues(values);
}
- 使用此功能时,您可以通过检查日期和时间来获取声誉更改。
注意:
- 这是一个示例脚本。因此,请根据您的实际情况对此进行修改。
- 不幸的是,在当前阶段,似乎API中没有用于自动通知何时获得新评论的方法。因此,作为Google Apps脚本的一种解决方法,我正在使用时间驱动的触发器来运行脚本。由此,我可以知道新的评论。也可以知道声誉的变化。
参考:
如果我误解了您的问题,而这不是您想要的结果,我深表歉意。
本文链接:https://www.f2er.com/3114790.html