解决使用 Gitalk 登录授权报错的问题
# 问题描述
Gitalk
登录GitHub
账号时,https://cors-anywhere.azm.workers.dev/https://github.com/login/oauth/access_token
接口出现Error:Network Error
, 这是因为 gitalk
在访问 github
时为了解决跨域问题使用了一个反向代理,这个默认的反向代理近期限制了访问,所以无法使用了。
# 解决方案
# Nginx代理
配置 nginx
添加以下内容到第一个 location
前面:
location = /login/oauth/access_token {
proxy_pass https://github.com;
}
1
2
3
2
3
例如我的网站地址是:http://zhenglinglu.cn,则使用下面配置:
location = /login/oauth/access_token {
add_header Access-Control-Allow-Origin 'http://zhenglinglu.cn';
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
if ($request_method = 'OPTIONS') {
return 204;
}
proxy_pass https://github.com;
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
我将反向代理部署到了 http://zhenglinglu.cn 这个网站上(其他网站无法使用,因为我配置了 Access-Control-Allow-Origin http://zhenglinglu.cn
),然后我需要在 gitalk
中添加以下内容:
proxy: 'http://zhenglinglu.cn/login/oauth/access_token'
1
# Node代理
const express = require('express');
const app = express();
const { createProxyMiddleware } = require('http-proxy-middleware');
app.use('/login/oauth/access_token', createProxyMiddleware({
target: 'https://github.com', //转发到的地址
changeOrigin: true, // needed for virtual hosted sites
//ws: true, // 代理websocket
// pathRewrite: {
// '^/': '/', // rewrite path
// }
}))
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 示例配置
plugins: [
[
'vuepress-plugin-comment', // 评论
{
choosen: 'gitalk',
options: {
clientID: 'xxxxxxxxx',
clientSecret: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
repo: 'zlluGitHub.github.io', // GitHub 仓库
owner: 'zlluGitHub', // GitHub仓库所有者
admin: ['zlluGitHub'], // 对仓库有写权限的人
// distractionFreeMode: false,
pagerDirection: 'last', // 'first'正序 | 'last'倒序
id: '<%- (frontmatter.permalink || frontmatter.to.path).slice(-16) %>', // 页面的唯一标识,长度不能超过50
// id: '<%- frontmatter.permalink || frontmatter.to.path %>', // 页面的唯一标识,长度不能超过50
// path: '<%- frontmatter.permalink || frontmatter.to.path %>',
title: '「评论」<%- frontmatter.title %>', // GitHub issue 的标题
labels: ['Gitalk', 'Comment'], // GitHub issue 的标签
// proxy:'https://netnr-proxy.cloudno.de/https://github.com/login/oauth/access_token',
proxy:'http://zhenglinglu.cn/login/oauth/access_token',
body:
'页面:<%- window.location.origin + (frontmatter.to.path || window.location.pathname) %>', // GitHub issue 的内容
}
}
]
]
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
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
上次更新: 2024/01/30, 00:35:17