Search code examples
jsonajaxsteamsteam-web-api

Steam Web API: Get CSGO inventory/ CrossDomainRequest


I am having issues making the ajax request from my localhost to http://steamcommunity.com/profiles/{steamid}/inventory/json/730/2

The issue seems to be that they do not have CORS headers enabled, so I have to use jsonp. Since the GET request returns json, but my ajax function is expecting json-p I receive an error:

Uncaught SyntaxError: Unexpected token :
2?callback=jQuery22005740937136579305_1452887017109&_=1452887017110:1 

I need this resource, but I am unsure how to get around this. I've looked around on SO, but haven't found anything that matches this issue specifically. There are a few sites that are able to obtain a specific user's inventory, so in some respect it has to be possible.

My Ajax call

    $.ajax({
    url: "http://steamcommunity.com/profiles/76561198064153275/inventory/json/730/2",
    type: 'GET',
    dataType: 'jsonp',
    success: function(response) {
        console.log(response);
        if(response.error){
            alert(response.error_text);
        }else {
            console.log("SUCCESS!!");
        }
    }
});

Solution

  • I have figured out a workaround! I am using django for my web application and so I tried to make the request server side.

    I installed the requests library through pip (The library: http://docs.python-requests.org/en/latest/)

    In my django app I made a view that would be called by my AJAX request

    def get_steam_inv(request):
         user_steam_profile = SteamProfile.objects.get(brokerr_user_id = request.user.id)
         r = requests.get("http://steamcommunity.com/profiles/76561198064153275/inventory/json/730/2")
         return JsonResponse(r.json())
    

    Then my ajax request for this view:

    $.ajax({
        url: "/ajax_get_steam_inv/",
        type: 'GET',
            success: function(response) {
                console.log(response);
                // result = JSON.parse(response);
                if (response.error){
                    alert(response.error_text);
                } else {
                    console.log(response);
                }
            }
    });
    

    And now I have the data I needed !