我正在构建一个颤振应用程序,该应用程序通过粒子云与粒子光子设备通信,但我无法向粒子云发出格式正确的请求。
当我尝试使用编码为表单数据的粒子访问令牌发送请求时,我的代码如下所示:
void setStatus(int idx) async {
print('ParticleService: setStatus($idx)');
var url = particleHost + _deviceID + particleVerb1;
var formData = 'access_token=$_accessToken¶ms=$idx';
var bodyLen = formData.length;
var headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": "$bodyLen"
};
var response = await http.post(url, body: formData, headers: headers);
}
有了它,我收到一个错误,表明它找不到访问令牌:
Response body: {"error":"invalid_request","error_description":"The access token was not found"}
当我尝试在授权标头中发送访问令牌时,它看起来像这样:
var url = particleHost + _deviceID + particleVerb1;
var body = json.encode({"params": "$idx"});
var bodyLen = body.length;
var headers = {
HttpHeaders.authorizationHeader: _accessToken,
HttpHeaders.contentTypeHeader: 'application/json',
"Content-Length": "$bodyLen"
};
print('URL: $url');
print('Body: $body');
print('Headers: $headers');
var response = await http.post(url, body: body, headers: headers);
粒子云回复 auth header 格式错误:
Response body: {"error":"invalid_request","error_description":"Malformed auth header"}
我以前做过很多次,效果很好,但是关于 Flutter 实现的一些事情让我很生气。这是 Ionic 应用程序的 TypeScript 中的内容:
setStatus(currentStatus: number): Promise<any> {
console.log(`ParticleService: setStatus(${currentStatus})`);
const url: string = PARTICLE_HOST + this.deviceID + PARTICLE_VERB_1;
const body = `access_token=${this.accessToken}¶ms=${currentStatus}`;
const headers: any = {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': body.length
};
return new Promise((resolve, reject) => {
this.http.post(url, body, { headers }).subscribe(data => {
resolve(data);
}, error => {
console.error(error.message);
reject(error.message);
});
});
}
这是浏览器的JavaScript:
console.log(`setRemoteStatus(${btnName})`);
// Get the color code based on the status being set
let colorCode = buttons.indexOf(btnName);
// Build the URL we'll use to talk to the Particle API
let postURL = particleHost + particlePath + config.getDeviceID() + particleVerb1;
// Craft the POST request body
let postBody = `access_token=${config.getAccessToken()}¶ms=${colorCode}`;
// Call the API
fetch(postURL, {
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': "application/x-www-form-urlencoded",
"Content-Length": postBody.length
},
referrerPolicy: 'no-referrer',
body: postBody
}).then(res => {
if (res.status == 200) {
displayAlert('Status Update', 'info', 'Successfully set remote status', 500);
} else {
displayAlert('Update Error', 'warning', `Status not set (result code ${res.status})`);
}
}).catch(err => {
console.error('Unable to set status');
displayAlert('Update Error', 'error', `Unable to set status (${err.message})`);
});
有人可以帮我理解我在 Dart 代码中哪里出错了吗?