我正在尝试使用Rest API调用Azure存储队列。我在访问API时遇到问题。我要403。不过不确定如何。
这是我不上类的代码游乐场代码:
async fn post_to_queue() {
let account_key = "access_key_key1";
let date = format!("{}", chrono::Utc::now().format("%a, %d %h %Y %T GMT"));
let date_header = format!("x-ms-date:{}\nx-ms-version:2015-02-21\n", date);
let inputvalue = format!(
"{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
"POST\n", /*VERB*/
"\n", /*Content-Encoding*/
"\n", /*Content-Language*/
"\n", /*Content-Length*/
"\n", /*Content-MD5*/
"\n", /*Content-Type*/
"\n", /*Date*/
"\n", /*If-Modified-Since*/
"\n", /*If-Match*/
"\n", /*If-None-Match*/
"\n", /*If-Unmodified-Since*/
"\n", /*Range*/
date_header, /*CanonicalizedHeaders*/
"/storage_name/queue_name/" /*CanonicalizedResource*/
);
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_name")
.version(actix_web::http::Version::HTTP_11)
.header("x-ms-date", date.as_str())
.header("x-ms-version", "2015-02-21")
.header(
"authorization",
format!("SharedKey storage_name:{}", signature).as_str(),
);
println!("Request: {:?}", req);
let response = req
.send_body(actix_web::dev::Body::from_slice(
b"<QueueMessage><MessageText>Yep ofc</MessageText></QueueMessage>",
))
.await;
println!("Response: {:?}", response);
}
我查看了This example,并正式使用了官方的documentation,但我不断获得403。我也查看了没有运气的非正式AzureSdkForRust。我的目标:将消息发布到存储队列中。而已。
有任何想法吗?
最佳答案
我解决了!
进行一些细微的更改,主要是URL中的/messages和添加内容 header
#[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());
}
关于azure - 使用Rust调用Azure Storage Queue Rest API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64053017/