ORCID Record GitHub Action
我做了一个GitHub Action,利用ORCID的公共API自动抓取研究人员的记录,包括其发表、作者、期刊和时间等等。因为原始API这些信息是位于不同URI下的,因此我进行了整合以写入到一个单独的JSON文件中。这个Action目前可以在GitHub Action市场中安装,用法如下。
ORCID API配置
1. 注册你的个人公共API客户端
首先登录你的ORCID账号,进入developer tools页面创建你的个人API客户端。详细的步骤请参考官方文档。应用信息和重定向地址可以随便填。
记住你的 Client ID 和 __Client secret__。
2. 获取你的访问令牌
在命令行中,用你的 Client ID 和 Client secret 来获取你的 __访问令牌__。官方目前默认给20年的超长有效期,因此获取这个令牌的命令只需要执行一次,我没有写到Action中:
1 2 3
| curl -H "Accept: application/json" -H "Content-Type: application/x-www-form-urlencoded" --data-urlencode "client_id=CLIENT_ID" --data-urlencode "client_secret=CLIENT_SECRET" --data-urlencode "scope=/read-public" --data-urlencode "grant_type=client_credentials" https://orcid.org/oauth/token
|
然后你就可以得到一个JSON格式的响应:
1
| {"access_token":"xxx","token_type":"bearer","refresh_token":"xxx","expires_in":631138518,"scope":"/read-public","orcid":null}
|
这里access_toke就是要记住的访问令牌。
输入
orcid-id
必须 研究人员的ORCID ID。
access-token
必须 上面获取到的访问令牌。
record-file
可选 要写入结果的JSON文件地址(相对于仓库根目录)。如果这个输入给了,record
输出就不会有。
输出
record
JSON格式的记录字符串。这个输出只有在record-file
输入没有的时候才会产生。
示例
1. (可选)在GitHub中保存你的访问令牌和变量
在 https://github.com/USERNAME/REPOSITORY/settings/secrets/actions 中创建新的仓库级别密钥(Secret)来保存你的访问令牌,这里我们将其命名为ORCID_ACCESS_TOKEN。
然后还是这个页面,切到Variables选项卡,创建以下的变量:
名称 | 描述 | 示例 |
---|
ORCID_ID | 研究人员的ORCID ID | XXXX-XXXX-XXXX-XXXX |
ORCID_ACCESS_TOKEN | 你的ORCID API访问令牌 | xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
RECORD_FILE | 要写入记录的JSON文件路径(可选,相对于仓库根目录) | assets/record.json |
2. 创建一个Action来自动更新ORCID记录
示例工作流代码如下:
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| name: Update Record
on: schedule: - cron: "0 0 1 * *" workflow_dispatch:
permissions: contents: write jobs: build: runs-on: ubuntu-latest
steps: - uses: actions/checkout@v4 - name: Get record with token uses: sxlllslgh/orcid-record-action@v1 id: record with: orcid-id: ${{ vars.ORCID_ID }} access-token: ${{ secrets.ORCID_ACCESS_TOKEN }} record-file: ${{ vars.RECORD_FILE }} - name: Make sure the record file is tracked run: git add ${{ vars.RECORD_FILE }}
- name: Judge if file changed id: changed continue-on-error: true run: git diff --exit-code ${{ vars.RECORD_FILE }}
- name: Judge if staged file changed id: cached continue-on-error: true run: git diff --exit-code --cached ${{ vars.RECORD_FILE }}
- name: Update record if: ${{ steps.changed.outcome == 'failure' || steps.cached.outcome == 'failure' }} run: | git config --global user.name '${{ vars.GIT_USERNAME }}' git config --global user.email '${{ vars.GIT_EMAIL }}' git commit -am "Automatically update record." git push
|