问题描述
我正在尝试使用Rest API调用Azure存储队列。我在访问API时遇到问题。我要403。不过不确定如何。
#Connection established with variable name oraCurs...
def returnaDict():
return {
#46 fields
'ID': '',# int
'STATUS': '',# String
#...
#...
}
#Query ran here
data = list()
for aFeature in query.features:
newDict = returnaDict()
newDict['ID'] = aFeature.attributes["ObjectID"]
newDict['STATUS'] = aFeature.attributes["Status"]
#...
data.append(newDict)
oraCurs.executemany("""Insert into table (ID,STATUS,...) Values (%s,%s,...)""",data)
oraCurs.execute('COMMIT')
我看过This example,而官方看过documentation,但我却一直保持403。
我的目标:将消息发布到存储队列中。就是这样。
有什么想法吗?
解决方法
我解决了! 进行了一些细微的更改,主要是URL中的/ messages和添加了内容标头
#[actix_rt::test]
async fn call_fucking_microsoft_the_shit_heads() {
let account_key = "my_key";
let date = format!("{}",chrono::Utc::now().format("%a,%d %h %Y %T GMT"));
let body = b"<QueueMessage><MessageText>Yep ofc</MessageText></QueueMessage>";
let inputvalue = format!(
"{}{}{}{}{}{}{}{}{}{}{}{}{}{}","POST\n",/*VERB*/
"\n",/*Content-Encoding*/
"\n",/*Content-Language*/
body.len().to_string() + "\n",/*Content-Length*/
"\n",/*Content-MD5*/
"application/x-www-form-urlencoded\n",/*Content-Type*/
"\n",/*Date*/
"\n",/*If-Modified-Since*/
"\n",/*If-Match*/
"\n",/*If-None-Match*/
"\n",/*If-Unmodified-Since*/
"\n",/*Range*/
format!("x-ms-date:{}\nx-ms-version:2019-12-12\n",date),/*CanonicalizedHeaders*/
"/my_storage/my_queue/messages" /*CanonicalizedResource*/
);
println!("to sign: {}",&inputvalue);
let key = ring::hmac::Key::new(
ring::hmac::HMAC_SHA256,&base64::decode(account_key).unwrap(),);
let sig = ring::hmac::sign(&key,inputvalue.as_bytes());
let signature = base64::encode(sig.as_ref());
let client = actix_web::client::Client::default();
let req = client
.post("https://storage_name.queue.core.windows.net/queue/messages")
.version(actix_web::http::Version::HTTP_11)
.header("x-ms-date",date.as_str())
.header("x-ms-version","2019-12-12")
.header("content-length",body.len().to_string())
.header("content-type","application/x-www-form-urlencoded")
.header(
"authorization",format!("SharedKey shipteststorage:{}",signature).as_str(),);
println!("Request: {:?}",req);
let response = req.send_body(actix_web::dev::Body::from_slice(body)).await;
println!("Response: {:?}",response);
assert!(response.is_ok());
assert!(response.unwrap().status().is_success());
}