我正在尝试从axios
发送请求,如下所示:
import * as vscode from "vscode";
import * as fs from "fs";
import * as path from "path";
import * as os from 'os';
import axios from 'axios';
import * as FormData from 'form-data';
/**
* Prefetch QRCode https://www.zhihu.com/api/v3/account/api/login/qrcode
* Get QRCode https://www.zhihu.com/api/v3/account/api/login/qrcode/${token}/image
* Query ScanInfo https://www.zhihu.com/api/v3/account/api/login/qrcode/${token}/scan_info
*/
const qrCodeUrl = "https://www.zhihu.com/api/v3/account/api/login/qrcode";
const uDIDUrl = 'https://www.zhihu.com/udid';
export class ZhihuService{
public async login(){
vscode.window.showInformationMessage("知乎登陆");
await this.qrcodeLogin();
}
protected async qrcodeLogin() {
var api = axios.create({
withCredentials: true
});
//pre-hand before get qrcode token
api.post(uDIDUrl, null,{
withCredentials:true
}).then(res =>{
console.log(api);
console.log(res.data);
}).catch(err =>{
console.log(err);
});
await api.post(qrCodeUrl, null,{
withCredentials:true
}).catch((error: any) =>{
console.log(error);
});
}
}
对于api.post(uDIDUrl
,它返回预期结果。
但是对于第二个请求,它总是返回403,如果我在postman中测试这两个reqeust url,它可以检查第二个请求中的错误,我可以看到第一个请求响应中的cookie没有自动附加。
任何建议都将不胜感激。
发布于 2021-06-29 04:49:02
在我看来,你应该先等第一个电话再打第二个。要么在第一个调用的then
子句中执行await
,要么执行第二个调用,因为它们相互依赖。
// A Promise will be returned for POST uDIDUrl (never assigned anywhere) and execution will continue
api.post(uDIDUrl, null,{
withCredentials:true
}).then(res =>{
console.log(api);
console.log(res.data);
}).catch(err =>{
console.log(err);
});
// The execution will block until the returned Promise of the `POST qrCodeUrl` request is fulfilled
await api.post(qrCodeUrl, null,{
withCredentials:true
}).catch((error: any) =>{
console.log(error);
});
发布于 2021-06-29 13:00:45
这里的问题是两个API调用是并行运行的。由于API依赖于uDIDUrl's
响应,因此您应该先等待第一次qrCodeUrl
调用完成。
为此,您有两个选择:
然后在uDIDUrl
uDIDUrl
请求中进行qrcodeLogin
)此外,您不需要再次添加withCredentials
,因为您在初始化axios实例时已经编写了一次。
import * as vscode from "vscode";
import * as fs from "fs";
import * as path from "path";
import * as os from 'os';
import axios from 'axios';
import * as FormData from 'form-data';
/**
* Prefetch QRCode https://www.zhihu.com/api/v3/account/api/login/qrcode
* Get QRCode https://www.zhihu.com/api/v3/account/api/login/qrcode/${token}/image
* Query ScanInfo https://www.zhihu.com/api/v3/account/api/login/qrcode/${token}/scan_info
*/
const qrCodeUrl = "https://www.zhihu.com/api/v3/account/api/login/qrcode";
const uDIDUrl = 'https://www.zhihu.com/udid';
export class ZhihuService {
public async login() {
vscode.window.showInformationMessage("知乎登陆");
await this.qrcodeLogin();
}
protected async qrcodeLogin() {
var api = axios.create({
withCredentials: true
});
//pre-hand before get qrcode token
await api.post(uDIDUrl).then(res => { // Added await here & removed extra parameters
console.log(api);
console.log(res.data);
}).catch(err => {
console.log(err);
});
await api.post(qrCodeUrl).catch((error: any) => {
console.log(error);
});
}
}
发布于 2021-06-17 16:48:20
我认为这两个请求是同时发生的,而不是在彼此之后。
https://stackoverflow.com/questions/68015899
复制相似问题