Sign In Register

How can we help you today?

Start a new topic

REST API, how to add metadata via Python script?

Hello,

 

I want to automate filling in metadata through a python script. It returns status code 400 ({"errorCode": "bad_request", "message": "Media not supported"}).

I took the code from this topic (https://support.gamesparks.net/support/discussions/topics/1000073290) and started redesigning it for myself. 


On this page I managed to add data https://docs.gamesparks.com/api-documentation/rest-apis/nosql.html


Can someone tell me what the error is?



  

import requests
import json
import os
from base64 import b64encode

userPass = b64encode(b"username:pass").decode("ascii")
gameId = "API"
stage = "preview"
collection = "meta.collectionName"
postURL = f"https://{gameId}.{stage}.cluster.gamesparks.net/restv2/game/{gameId}/mongo/collection/{collection}/insert"

auth = {"Authorization" : "Basic " + userPass}

postData = requests.post(postURL, headers=auth, data={"document":json.dumps({"test":2})})

print (postData.status_code)

  


there is a feeling that you need to receive a jwt-token, but I try in different ways, it still does not work.

I can get the token, but I can't add something to the metadata

The problem is solved, you need to get a token and in the post request you need to transfer data to the json parameter, it adds the necessary headers and converts the data to json, when I did it all manually it did not work.

Maybe someone will be helpful, here is the working code

import requests
import json
import os
from base64 import b64encode

def get_jwt():
  get_url_jwt = f"https://auth.gamesparks.net/restv2/auth/game/{GAME_ID}/jwt/nosql"
  get_jwt_data = requests.get(get_url_jwt, headers=auth)

  return get_jwt_data.json()['X-GS-JWT']

root_path = os.path.dirname(os.path.abspath(__file__))
buildings_json_folder = "server_buildings"

buildings_json_path = os.path.join(root_path, buildings_json_folder)

GS_LOGIN = os.getenv("GS_LOGIN")
GS_PASS = os.getenv("GS_PASS")
GAME_ID = os.getenv("GAME_ID")

user_pass = bytes(f"{GS_LOGIN}:{GS_PASS}", "ascii")
user_pass = b64encode(user_pass).decode("ascii")
auth = {"Authorization" : f"Basic {user_pass}"}
stage = "preview"
collection = "meta.buildings"

GS_JWT = get_jwt()

header_access = {"X-GS-JWT": GS_JWT}

post_url = f"https://{GAME_ID}.{stage}.cluster.gamesparks.net/restv2/game/{GAME_ID}/mongo/collection/{collection}/insert"
response_post = requests.post(post_url, headers=header_access, json=building_data)
print(file, response_post.ok)

 

Login to post a comment