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');
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) => {
request({
method: 'POST',
url: 'https://gitee.com/oauth/token',
form: {
client_id: clientId,
client_secret: conf.values.giteeClientSecret,
client_secret: clientSecret,
code,
grant_type: 'authorization_code',
redirect_uri: oauth2RedirectUri,

View File

@ -69,7 +69,8 @@ export default {
* https://developer.gitee.com/apps/building-oauth-apps/authorization-options-for-oauth-apps/
*/
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;
if (!silent) {
// Get an OAuth2 code
@ -105,13 +106,21 @@ export default {
}
const accessToken = tokenBody.access_token;
// Call the user info endpoint
const user = (await networkSvc.request({
let user = null;
try {
user = (await networkSvc.request({
method: 'GET',
url: 'https://gitee.com/api/v5/user',
params: {
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')) {
user.avatar_url = `${user.avatar_url}!avatar60`;
}
@ -194,6 +203,7 @@ export default {
repo,
branch,
}) {
try {
const refreshedToken = await this.refreshToken(token);
const { commit } = await repoRequest(refreshedToken, owner, repo, {
url: `commits/${encodeURIComponent(branch)}`,
@ -205,6 +215,12 @@ export default {
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;
}
},
async checkAndCreateRepo(token) {