gitee接口限制问题解决
This commit is contained in:
parent
9f800ecece
commit
e1eb7ec350
@ -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,
|
||||||
|
@ -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;
|
||||||
|
try {
|
||||||
|
user = (await networkSvc.request({
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
url: 'https://gitee.com/api/v5/user',
|
url: 'https://gitee.com/api/v5/user',
|
||||||
params: {
|
params: {
|
||||||
access_token: accessToken,
|
access_token: accessToken,
|
||||||
},
|
},
|
||||||
})).body;
|
})).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,6 +203,7 @@ export default {
|
|||||||
repo,
|
repo,
|
||||||
branch,
|
branch,
|
||||||
}) {
|
}) {
|
||||||
|
try {
|
||||||
const refreshedToken = await this.refreshToken(token);
|
const refreshedToken = await this.refreshToken(token);
|
||||||
const { commit } = await repoRequest(refreshedToken, owner, repo, {
|
const { commit } = await repoRequest(refreshedToken, owner, repo, {
|
||||||
url: `commits/${encodeURIComponent(branch)}`,
|
url: `commits/${encodeURIComponent(branch)}`,
|
||||||
@ -205,6 +215,12 @@ export default {
|
|||||||
throw new Error('Git tree too big. Please remove some files in the repository.');
|
throw new Error('Git tree too big. Please remove some files in the repository.');
|
||||||
}
|
}
|
||||||
return tree;
|
return tree;
|
||||||
|
} catch (err) {
|
||||||
|
if (err.status === 401) {
|
||||||
|
this.startOauth2();
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
async checkAndCreateRepo(token) {
|
async checkAndCreateRepo(token) {
|
||||||
|
Loading…
Reference in New Issue
Block a user