Sign In Register

How can we help you today?

Start a new topic
Answered

Creating downloadables using REST API

Hi,


Platform Release 2015-07-02 introduced the creation of Downloadables from the REST API (user story 2833). Maybe I'm missing something but I cannot find any documentation on this. Can this feature be used and if so, what's the URL and parameters?


Our use case is to upload quiz images from a script or content management web form. If there's another/better approach to doing this I'd love to hear it as well. Only other path I can think of is to have the script login as a user using the JavaScript SDK, create a bunch of `UploadUrlRequest`s and then use those to upload content.


Best Answer

Hi Ivo,


Yes you can indeed use REST to create downloadables, we are currently revamping our documentation and hope to include the details of this new feature soon. I use Postman for REST calls myself, its a Chrome App, here is the configuration I used.



As you can see its a POST request with a binaryContentFile parameter, the url is "https://portal.gamesparks.net/rest/"yourGameApiKey"/binarycontent/TEST" , TEST is the shortCode name to be given to the downloadable file. If you have any more questions just let me know.


Thanks,

Liam



Answer

Hi Ivo,


Yes you can indeed use REST to create downloadables, we are currently revamping our documentation and hope to include the details of this new feature soon. I use Postman for REST calls myself, its a Chrome App, here is the configuration I used.



As you can see its a POST request with a binaryContentFile parameter, the url is "https://portal.gamesparks.net/rest/"yourGameApiKey"/binarycontent/TEST" , TEST is the shortCode name to be given to the downloadable file. If you have any more questions just let me know.


Thanks,

Liam


Thanks Liam, this is working great! I can create and also update existing Downloadables using the same call. Is there any way to remove/delete Downloadables using the REST API as well?

hi Ivo, 
You can delete downloadables using the delete call and the following url: 


https://portal.gamesparks.net/rest/{api key}/binarycontent/{downloadable shortcode}


There is a bug at the moment where empty downloadables can't be deleted until content is added just to beware of that. 


let me know how this works for you.

Regards Patrick. 

Excellent, works like a charm!

No problem Ivo glad I could help. 

Regards Patrick. 

I'm Trying to upload asset bundles to GameSparks from inside the Unity editor.  

static void UploadBundles(string platform)
    {
		string directoryPath = Application.dataPath + "/Game/AssetBundles" + platform;

		string[] files = Directory.GetFiles(directoryPath);

		int count = 0;

		for ( int i = 0; i < files.Length; i++ )
		{
			EditorUtility.DisplayProgressBar("Uploading for " + platform, i.ToString("000"), ((float)i) / files.Length);

			var path = files[i];

			var extension = Path.GetExtension(path);
			var filename = Path.GetFileName(path);

			//Upload all files with no extension
			if ( string.IsNullOrEmpty(extension) ) {

				//
				try {

					var loginString = System.Text.Encoding.UTF8.GetBytes(USERNAME+":"+PASSWORD);
					var encodedLogin = System.Convert.ToBase64String(loginString);
					var login = "Basic " + encodedLogin;

					var headers = new Dictionary<string,string>();

					headers["Access-Control-Allow-Origin"] = "*";
					headers["Content-Type"] = "multipart/form-data";//"application/octet-stream";
					headers["Authorization"] = login;

					byte[] data = File.ReadAllBytes(files[i]);
					WWWForm form = new WWWForm();
					form.AddBinaryData("binaryContentFile",data,filename,"multipart/form-data");//"application/octet-stream");

					var url = "https://portal.gamesparks.net/rest/"+API_KEY+"/binarycontent/" + filename+"_"+platform.ToLower();
					WWW www = new WWW(url,form.data,headers);

					while(!www.isDone);

					if ( !string.IsNullOrEmpty(www.error) ) {
						Debug.LogError("Error Uploading file " + filename);
						Debug.LogError(www.error);
						foreach ( var pair in www.responseHeaders)
						{
								Debug.LogError(pair.Key + " : " + pair.Value);
						}
						break;
					}
					else
					{
					foreach ( var pair in www.responseHeaders)
					{
						Debug.Log(pair.Key + " : " + pair.Value);
					}
					Debug.Log(www.text);
					}

					break;

				} catch (System.Exception e) {
					Debug.LogError("Exception: " + e.Message);
					break;
				}

				count += 1;

			}

		}

		EditorUtility.ClearProgressBar();

		Debug.Log("Uploaded " + count);
    }

  The above code returns 500 Server Error every time. Any idea why?


Hi


It seems the answer is for old rest API

What is the new request url for new rest API?


I tried https://config2.gamesparks.net/restv2/game/{API_KEY}/binarycontent/TEST, but got "not_allowed".


Jo

Hi Jo,


The Following URL will allow you to add binary data to a Downloadable you have pre-configured.

https://config2.gamesparks.net/restv2/game/YOURAPIKEY/config/~downloadables/SHORTCODE/file


If you have not already configured a downloadable you can use the following to do so.

https://config2.gamesparks.net/restv2/game/YOURAPIKEY/config/~downloadables


You can learn more about these two REST APIs here and here.


Regards,

Ronan


1 person likes this

Hi Ronan,


The REST API docs for creating downloadables could use some further explanation.


For instance:


  1. In order to upload file data, do you need to issue two requests (first POST ~downloadables with the meta data, and then POST ~downloadables/{downloadableId} with the file contents)? Is there any way to make this as one request or does it always need two?
  2. When creating a downloadable (POST ~downloadables), what metadata fields are required and which should be omitted? Is just "shortCode" enough? Or "shortCode" and "fileName"? Or...?  Should other fields be omitted?

Thanks,
Gordon
Login to post a comment