Help regarding google cloud vision api in lua

108 views
Skip to first unread message

Rana Ayaz

unread,
Nov 4, 2024, 2:18:31 AMNov 4
to lu...@googlegroups.com
I have created a new account on Google Cloud.
Enabled the Google Cloud Vision API in the project.
Create a new Credential
Then create a new API for this project.
after completing all this process when i am using this api key in my code it is giving error.
"error": {
"code": 400,
"message": "API key not valid. Please pass a valid API key.",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
"reason": "API_KEY_INVALID",
"domain": "googleapis.com",
"metadata": {
"service": "vision.googleapis.com"

The API is saying invalid although I copied the API from the same file and used it in the code which was given to us by Google to download..
Am sending my code below plz guide me where i am doing wrong or how to make full correct url.
And also tell if any other option or any other service has to be activated to use the API in this code..
Below I am posting my code
In this code below I have removed the API key.

require "import"
local crypt = require "crypt"
import "cjson"
local cjson = require "cjson"

api_key = ""
local url = "https://vision.googleapis.com/v1/images:annotate?key=" .. api_key

-- تصویر کا راستہ
local image_path = "/storage/emulated/0/folder/file2.jpg"

-- تصویر کو بیس64میں تبدیل کرنے کا عمل
local function encodeFileToBase64(file_path)
local file = io.open(file_path, "rb") -- فائل کو پڑھنے کے لیے کھولیں
if not file then return nil end
local content = file:read("*a") -- پورا مواد پڑھیں
file:close()
return crypt.base64encode(content) -- میں بیس64تبدیل کریں
end

-- تصویر کا بیس64ڈیٹا حاصل کریں
local image_base64 = encodeFileToBase64(image_path)

-- درخواست کی باڈی تیار کریں
local request_body = cjson.encode({
requests = {
{
image = {
content = image_base64 -- ڈیٹا شامل بیسکریں64
},
features = {
{
type = "LABEL_DETECTION",
maxResults = 10
}
}
}
}
})

-- HTTP POST درخواست بھیجیں
Http.post(url, request_body, { ["Content-Type"] = "application/json" }, function(status, data)
if status == 200 then
print(data) -- API کا جواب دکھائیں
else
print("Error: " .. data) -- خطا کی صورت میں پیغام دکھائیں
end
end)

Sainan

unread,
Nov 4, 2024, 2:27:47 AMNov 4
to lu...@googlegroups.com
Typically the way you authenticate with such APIs is by giving an `Authorization` header, not a `key` GET parameter, so you'll probably want it to look something like this:

Http.post(url, request_body, {
["Authorization"] = "Bearer " .. api_key, -- حظا سعيدا، الصياد
["Content-Type"] = "application/json"
}, ...)

Next time, read the manual: https://cloud.google.com/docs/authentication/rest

-- Sainan

Rana Ayaz

unread,
Nov 4, 2024, 12:52:55 PMNov 4
to lu...@googlegroups.com

Thank you very much for your reply.
Just now I understand that we need to set authorization ie access token here as well.
I got the access token for it.
Have set it in the code and run it now the code is working perfectly.
But there is a huge problem.
The access token we receive comes with an expiration of only one hour.
What is a better solution to increase its expiration time?.
Can it not be acquired for lifetime?.
Because getting token every time and the process of getting it is also very complicated which takes time.
Dear Sir, please suggest a solution for this so that once I set it in the code, I don't have to replace it again and again and when I run the code, the code works correctly..


--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/J3ertmgVey8hCZnAcfe1mE_dXh1rV2gg1oZlfqf1VIEDCzGb0bi99fAEQGSiWl1Fr864MCd6m8KJP9Ry5jKW0bzkrYbNldrL12EbX1YSRMc%3D%40calamity.inc.

Sainan

unread,
Nov 4, 2024, 1:32:32 PMNov 4
to lu...@googlegroups.com
Good luck, have fun.

-- Sainan

Thijs Schreijer

unread,
Nov 5, 2024, 11:20:13 AMNov 5
to lu...@googlegroups.com


On Mon, 4 Nov 2024, at 18:52, Rana Ayaz wrote:

Thank you very much for your reply.
Just now I understand that we need to set authorization ie access token here as well.
I got the access token for it.
Have set it in the code and run it now the code is working perfectly.
But there is a huge problem.
The access token we receive comes with an expiration of only one hour.
What is a better solution to increase its expiration time?.
Can it not be acquired for lifetime?.
Because getting token every time and the process of getting it is also very complicated which takes time.
Dear Sir, please suggest a solution for this so that once I set it in the code, I don't have to replace it again and again and when I run the code, the code works correctly..


You cannot. The server determines validity. That is the whole point of this type of security, to have short-lived tokens only.

Thijs

Foster

unread,
Nov 5, 2024, 11:49:42 AMNov 5
to lua-l

This will happen if you click "Regenerate key" on a normal key, which, when created, should exist forever. If you do this once, though, it enters a state where it needs to be updated every 24 hours.

You can create a new key if you did this by accident, and if you don't click "Regenerate key", it should never expire.

This is for the GoogleAPI keys that I use, I don't know if there is something specific to the Vision server. 

Rana Ayaz

unread,
Nov 6, 2024, 4:15:20 AMNov 6
to lu...@googlegroups.com
But this is a big problem because the access token which is available for one hour can be taken by anyone by logging in with their email, there is no need to create an account and get an API. A link is used for any project, for example, the link used for Google Cloud Vision or Google Maps is a universal link that anyone can use..
So what's the difference between someone who creates an account and someone who gets a simple token?.
Dear sir, can you please help me further in this matter to create a function in lua by which we can get our access token by giving our fresh token..
Or suggest a way in which we can obtain a long-life access token by providing our private key and service account information..

--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.

Thijs Schreijer

unread,
Nov 6, 2024, 7:19:33 AMNov 6
to lu...@googlegroups.com


On Wed, 6 Nov 2024, at 10:15, Rana Ayaz wrote:
But this is a big problem because the access token which is available for one hour can be taken by anyone by logging in with their email, there is no need to create an account and get an API. A link is used for any project, for example, the link used for Google Cloud Vision or Google Maps is a universal link that anyone can use..
So what's the difference between someone who creates an account and someone who gets a simple token?.
Dear sir, can you please help me further in this matter to create a function in lua by which we can get our access token by giving our fresh token..
Or suggest a way in which we can obtain a long-life access token by providing our private key and service account information..


I don't know the API your referring to, but here's one I did: https://tieske.github.io/millheat.lua/modules/millheat.html

It instantiates a session with user creds. It keeps refresh and access tokens cached. If access token fails, it automatically refreshes, if refresh fails, it automatically logs in again.

This does however require quite some networking, so depending on your application this might not be something easy to implement.

hth

Rana Ayaz

unread,
Nov 6, 2024, 7:48:21 AMNov 6
to lu...@googlegroups.com
No I have generated only one API in my project.
While creating the API, I was given a JSON file to download..
This file contains the service account information and private key.
When I use this private key in my code it shows me an invalid api key error.
For this reason I always have to use an access token in my lua code which only comes with a one hour expiry..
So there is no point for me to create an account and create an API.
I heard somewhere that we can generate our lifetime token with the help of this file which we get to download while creating api..
But I don't know how we can get our lifetime access token from this JSON file which doesn't need to be updated frequently..

--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.

Rana Ayaz

unread,
Nov 6, 2024, 8:11:29 AMNov 6
to lu...@googlegroups.com
No I don't think it will work properly with Google Cloud API either because it is built around their own API..
Have you checked it with google cloud api does it work fine?.

--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages