gitee接口限制问题解决

This commit is contained in:
xiaoqi.cxq 2022-07-14 20:48:42 +08:00
parent 9f800ecece
commit e1eb7ec350
2 changed files with 37 additions and 19 deletions

View File

@ -3,13 +3,15 @@ const request = require('request');
const conf = require('./conf'); const conf = require('./conf');
function giteeToken(clientId, code, oauth2RedirectUri) { function giteeToken(clientId, code, oauth2RedirectUri) {
const clientIndex = conf.values.giteeClientId.split(',').indexOf(clientId);
const clientSecret = conf.values.giteeClientSecret.split(',')[clientIndex];
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
request({ request({
method: 'POST', method: 'POST',
url: 'https://gitee.com/oauth/token', url: 'https://gitee.com/oauth/token',
form: { form: {
client_id: clientId, client_id: clientId,
client_secret: conf.values.giteeClientSecret, client_secret: clientSecret,
code, code,
grant_type: 'authorization_code', grant_type: 'authorization_code',
redirect_uri: oauth2RedirectUri, redirect_uri: oauth2RedirectUri,

View File

@ -69,7 +69,8 @@ export default {
* https://developer.gitee.com/apps/building-oauth-apps/authorization-options-for-oauth-apps/ * https://developer.gitee.com/apps/building-oauth-apps/authorization-options-for-oauth-apps/
*/ */
async startOauth2(lastToken, silent = false, isMain) { async startOauth2(lastToken, silent = false, isMain) {
const clientId = store.getters['data/serverConf'].giteeClientId; const giteeClientIds = store.getters['data/serverConf'].giteeClientId.split(',');
const clientId = giteeClientIds[Math.floor((giteeClientIds.length * Math.random()))];
let tokenBody; let tokenBody;
if (!silent) { if (!silent) {
// Get an OAuth2 code // Get an OAuth2 code
@ -105,13 +106,21 @@ export default {
} }
const accessToken = tokenBody.access_token; const accessToken = tokenBody.access_token;
// Call the user info endpoint // Call the user info endpoint
const user = (await networkSvc.request({ let user = null;
method: 'GET', try {
url: 'https://gitee.com/api/v5/user', user = (await networkSvc.request({
params: { method: 'GET',
access_token: accessToken, url: 'https://gitee.com/api/v5/user',
}, params: {
})).body; access_token: accessToken,
},
})).body;
} catch (err) {
if (err.status === 401) {
this.startOauth2();
}
throw err;
}
if (user.avatar_url && user.avatar_url.endsWith('.png') && !user.avatar_url.endsWith('no_portrait.png')) { if (user.avatar_url && user.avatar_url.endsWith('.png') && !user.avatar_url.endsWith('no_portrait.png')) {
user.avatar_url = `${user.avatar_url}!avatar60`; user.avatar_url = `${user.avatar_url}!avatar60`;
} }
@ -194,17 +203,24 @@ export default {
repo, repo,
branch, branch,
}) { }) {
const refreshedToken = await this.refreshToken(token); try {
const { commit } = await repoRequest(refreshedToken, owner, repo, { const refreshedToken = await this.refreshToken(token);
url: `commits/${encodeURIComponent(branch)}`, const { commit } = await repoRequest(refreshedToken, owner, repo, {
}); url: `commits/${encodeURIComponent(branch)}`,
const { tree, truncated } = await repoRequest(refreshedToken, owner, repo, { });
url: `git/trees/${encodeURIComponent(commit.tree.sha)}?recursive=1`, const { tree, truncated } = await repoRequest(refreshedToken, owner, repo, {
}); url: `git/trees/${encodeURIComponent(commit.tree.sha)}?recursive=1`,
if (truncated) { });
throw new Error('Git tree too big. Please remove some files in the repository.'); if (truncated) {
throw new Error('Git tree too big. Please remove some files in the repository.');
}
return tree;
} catch (err) {
if (err.status === 401) {
this.startOauth2();
}
throw err;
} }
return tree;
}, },
async checkAndCreateRepo(token) { async checkAndCreateRepo(token) {