Making an API request (translations)

Hello, hope you are feeling well! I am stuck using Google’s API. I read everything already, first idk what happens to the given link that it doesn’t work, second idk which one should I use between:

HttpService:Post()
or
HttpService:RequestAsync()

Which one should I use to make the request and what’s the proper link for it?


Method: projects.translateText  |  Cloud Translation  |  Google Cloud

1 Like

API docs says post.

2 Likes

:RequestAsync() to reqest data, clearly.
You should find info on their docs, check their API, you’ll find answers!

2 Likes

I used the link but it doesn’t work…

1 Like

In case you want to know what the error is when I use that link is: 15:06:46.949 - HttpError: InvalidUrl

1 Like

Did you issue a POST request? What is the HTTP status code? Did you follow the syntax requirements?

See: https://github.com/googleapis/googleapis/blob/master/google/api/http.proto for those requirements.

Have you tried official support? How to get help  |  Google APIs Explorer  |  Google for Developers

2 Likes
local HttpService = game:GetService("HttpService")
local TranslatorLink = "https://translation.googleapis.com/v3/{parent=projects/*}:translateText"

local Data = {
	contents = {
		"Hello World",
	},
	sourceLanguageCode = "en",
	targetLanguageCode = "sr",
}

Data = HttpService:JSONEncode(Data)

wait(3)
local TranslatedString = HttpService:PostAsync(TranslatorLink, Data)
TranslatedString = HttpService:JSONDecode(TranslatedString).translatedText
print(TranslatedString)

This the script I have, while following everything they said.

1 Like

I presume you’ve removed the API information.

If not,

Format: projects/{project-number-or-id} or projects/{project-number-or-id}/locations/{location-id} .

For global calls, use projects/{project-number-or-id}/locations/global or projects/{project-number-or-id} .

Refer to this and adapt as required.

2 Likes

The error I am currently having is the one of the URL, I can’t check if I have errors setting up, if the link doesn’t proceed…

1 Like

See:

You must change the asterisk in your URL to a project’s id. You can create a project on the Cloud Resource Manager dashboard and see it’s id there.


FYI: You’re missing the mimeType in your request body, this should be "text/plain".

1 Like

Does it cost to create a project?

Nope, I have one for Firebase which doesn’t cost anything at all.

How do I create a project there, I have to click Get Started?