Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Having Issues with authentication using REST API

Hello,

I'm building a rust application that needs to use cloud vision ocr. I use service account key JSON file to authenticate, and I had no problems with it using the Python and Go libraries. I wasn't able to understand how to use this authentication method in the rust library, so I decided to use the POST request method. To do such thing I need to somehow include my key in the request header, and I have not been able to do it. My code and the response I get are below.

``` 

pub async fn req(url: &str-> Result<StringBox<dyn std::error::Error>> {
let auth_token = fs::read_to_string(String::from("auth_sin.json"))
.expect("Should have been able to read the file");
let client = reqwest::Client::builder().build()?;
let reqtext = format!("Bearer -d {}",&auth_token[..]);
let request = client
.header(String::from("Authorization"), reqtext.replace("\n",""))
.header("Content-Type""application/json; charset=utf-8")
.json(&build_request(url));
println!("{:?}"request);
let response =request
.send()
.await?;
let t = response
.text()
.await?;
Ok(t)
}
```
 
Reponse 
```
{
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED",
"details": [
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
"reason": "ACCESS_TOKEN_TYPE_UNSUPPORTED",
"metadata": {
"method": "google.cloud.vision.v1.ImageAnnotator.BatchAnnotateImages",
"service": "vision.googleapis.com"
}
}
]
}
}
```
 

 

0 1 4,584
1 REPLY 1

is this the documentation you followed?