Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1

    Battlefield 1 API

    Is there going to be an API for Battlefield 1?

    Im patiently waiting... here is me waiting....

    with my coffee...


  2. #2
    Administrator 1ApRiL's Avatar
    Join Date
    2011-02-12
    Location
    Waiblingen, Germany
    Posts
    1,525
    BFBC2 PC
    1ApRiL
    NFS World
    1APRIL
    BF2
    1ApRiL
    BF3 PC
    1ApRiL

    Re: Battlefield 1 API

    I don't know if we will offer an API, but there is a JSON RPC API on the official website:
    POST https://companion-api.battlefield.co...mesByPersonaId
    Admin of bfhstats.com / bf4stats.com / bf3stats.com | Twitter

  3. #3

    Re: Battlefield 1 API

    Quote Originally Posted by 1ApRiL View Post
    I don't know if we will offer an API, but there is a JSON RPC API on the official website:
    POST https://companion-api.battlefield.co...mesByPersonaId
    Hi! Just a question, can we access this with jQuery since it's on another domain?

  4. #4

    Re: Battlefield 1 API

    Hi,

    How can I get some data through the API?
    The link you posted is leading nowhere.

    Thank you

  5. #5

    Re: Battlefield 1 API

    Quote Originally Posted by theManag3R View Post
    Hi,

    How can I get some data through the API?
    The link you posted is leading nowhere.

    Thank you
    That is no REST api, it requires a little bit more to get results.
    If you have Google Chrome you can get the Postman extension to send requests to this api.
    1. Go to
      https://companion-api.battlefield.com/companion
      and login via the web interface.
    2. Open the Chrome developer tools with F12 and go to network
    3. On the bottom of the companion website click on CARRER
    4. In the developer tools Network tab click on the link to api?Stats.getCareerForOwnedGamesByPersonaId
    5. Now you need Postman, you should find it here chrome://apps/
    6. You can skip the account creation it is not needed
    7. Enter the url https://companion-api.battlefield.co...mesByPersonaId
    8. Change the left dropdown from GET to POST
    9. Go to Headers
    10. Copy from the developer tools: X-GatewaySession (that is the key and the string after the : is the value)
    11. Go to Body
    12. Copy from the developertools the Request Payload (after you click on 'view source')
    13. In Postman click on Send


    Hope this helps to understand a bit of it
    Don't know if there is an offical docu for that api but that way you can reverse engineer some parts.

  6. #6

    Re: Battlefield 1 API

    curl request is like this:

    curl 'https://companion-api.battlefield.com/jsonrpc/web/api?Stats.getCareerForOwnedGamesByPersonaId' -H 'Origin: https://www.battlefield.com' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36' -H 'Content-Type: application/json' -H 'Accept: */*' -H 'Cache-Control: max-age=0' -H 'Referer: https://www.battlefield.com/companion/career' -H 'Connection: keep-alive' -H 'X-GatewaySession: YOURSESSIONID' --data-binary '{"jsonrpc":"2.0","method":"Stats.getCareerForOwne dGamesByPersonaId","params":{"personaId":"YOURPERS ONAID"},"id":"YOURID"}' --compressed > stats.json
    Last edited by HigH_HawK; 2018-01-12 at 10:43. Reason: Removed URL TAGs

  7. #7

    Re: Battlefield 1 API

    Thank you, I'll make sure to check this out. I'd like to make a Python script to parse the data out of the api to my own mongodb and make a spreadsheet for me and my friends to see the stats.
    Is there a way to automate the Postman?

  8. #8

    Re: Battlefield 1 API

    Quote Originally Posted by H3iMDAll87 View Post
    curl request is like this:

    curl 'https://companion-api.battlefield.com/jsonrpc/web/api?Stats.getCareerForOwnedGamesByPersonaId' -H 'Origin: https://www.battlefield.com' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36' -H 'Content-Type: application/json' -H 'Accept: */*' -H 'Cache-Control: max-age=0' -H 'Referer: https://www.battlefield.com/companion/career' -H 'Connection: keep-alive' -H 'X-GatewaySession: YOURSESSIONID' --data-binary '{"jsonrpc":"2.0","method":"Stats.getCareerForOwne dGamesByPersonaId","params":{"personaId":"YOURPERS ONAID"},"id":"YOURID"}' --compressed > stats.json
    Wow, where did you get this? I'll test it to see what happens..
    Thank you!

  9. #9

    Re: Battlefield 1 API

    Quote Originally Posted by H3iMDAll87 View Post
    curl request is like this:

    curl 'https://companion-api.battlefield.com/jsonrpc/web/api?Stats.getCareerForOwnedGamesByPersonaId' -H 'Origin: https://www.battlefield.com' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36' -H 'Content-Type: application/json' -H 'Accept: */*' -H 'Cache-Control: max-age=0' -H 'Referer: https://www.battlefield.com/companion/career' -H 'Connection: keep-alive' -H 'X-GatewaySession: YOURSESSIONID' --data-binary '{"jsonrpc":"2.0","method":"Stats.getCareerForOwne dGamesByPersonaId","params":{"personaId":"YOURPERS ONAID"},"id":"YOURID"}' --compressed > stats.json
    Fantastic.. let me check it out..

  10. #10

    Re: Battlefield 1 API

    if this is helpfull, please drop a donation to this forum @ https://p-stats.com/imprint

  11. #11
    New User bArtAA's Avatar
    Join Date
    2011-10-08
    Location
    The Netherlands
    Posts
    11
    BFBC2 PC
    bArtAA
    BF2
    [DH]bArtAA
    BF3 PC
    bArtAA

    Re: Battlefield 1 API

    any news on a working api for guys like me not so into programming?

  12. #12

    Re: Battlefield 1 API

    asap, just wait until servers are aviable @ November

  13. #13

    Re: Battlefield 1 API

    Quote Originally Posted by H3iMDAll87 View Post
    curl request is like this:

    curl 'https://companion-api.battlefield.com/jsonrpc/web/api?Stats.getCareerForOwnedGamesByPersonaId' -H 'Origin: https://www.battlefield.com' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36' -H 'Content-Type: application/json' -H 'Accept: */*' -H 'Cache-Control: max-age=0' -H 'Referer: https://www.battlefield.com/companion/career' -H 'Connection: keep-alive' -H 'X-GatewaySession: YOURSESSIONID' --data-binary '{"jsonrpc":"2.0","method":"Stats.getCareerForOwne dGamesByPersonaId","params":{"personaId":"YOURPERS ONAID"},"id":"YOURID"}' --compressed > stats.json
    Hi,

    It seems I'm doing something wrong, since the curl won't display any data. PersonaID is fine, but what is my "YOURID"? At this point I'v only been trying to shove my gamertag on it and it seems that this is incorrect...
    Is the curl any different if the platform is PS4 / PC / XONE?
    SessionID works fine as well.
    I'll make sure to donate when I get this working.

    Thank you

  14. #14

    Re: Battlefield 1 API

    Quote Originally Posted by theManag3R View Post
    Hi,

    It seems I'm doing something wrong, since the curl won't display any data. PersonaID is fine, but what is my "YOURID"? At this point I'v only been trying to shove my gamertag on it and it seems that this is incorrect...
    Is the curl any different if the platform is PS4 / PC / XONE?
    SessionID works fine as well.
    I'll make sure to donate when I get this working.

    Thank you
    The GatewaySessionId which I don't think it can be generated by us.

    If you use localStorage.gatewaySessionId in the JS console while on the companion website it'll show you the session ID as well, unfortunately it's not static.

    EDIT: Nevermind, I read wrong. The ID is another parameter given to you when you log in into the web app, you can use the same one unlinke the GatewaySessionId.
    Last edited by itsTripleD; 2016-10-27 at 09:59.

  15. #15

    Re: Battlefield 1 API

    Quote Originally Posted by itsTripleD View Post
    The GatewaySessionId which I don't think it can be generated by us.

    If you use localStorage.gatewaySessionId in the JS console while on the companion website it'll show you the session ID as well, unfortunately it's not static.

    EDIT: Nevermind, I read wrong. The ID is another parameter given to you when you log in into the web app, you can use the same one unlinke the GatewaySessionId.
    The id parameter doesn't seem to be matter. I've been generating random guids for it and it's fine.

    I've scripted the entire process and it's working fine.

    After you login to the EA account, you need to get an auth token from the EA accounts page.

    Code:
            eaAuthClient.BaseUrl = new Uri("https://accounts.ea.com");
    
                var tokenAuth = new RestRequest("connect/auth?client_id=sparta-companion-web&response_type=code&prompt=none&redirect_uri=nucleus:rest");
                tokenAuth.AddQueryParameter("client_id", "sparta-companion-web");
                tokenAuth.AddQueryParameter("response_type", "code");
                tokenAuth.AddQueryParameter("prompt", "none");
                tokenAuth.AddQueryParameter("redirect_uri", "nucleus:rest");
                tokenAuth.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
    
    var tokenResponse = eaAuthClient.Execute<dynamic>(tokenAuth);

    After that, you take that token, and pass it to an endpoint within the companion API. This will return your gateway id that you can stuff in the header.

    Code:
                eaAuthClient.BaseUrl = new Uri("https://companion-api.battlefield.com");
    
                var loginViaTokenRequest = new RestRequest("jsonrpc/web/api?Companion.loginFromAuthCode", Method.POST);
                loginViaTokenRequest.RequestFormat = DataFormat.Json;
                
                loginViaTokenRequest.AddBody(
                    new
                    {
                        jsonrpc = "2.0",
                        method = "Companion.loginFromAuthCode",
                        @params = new { code = tokenResponse.Data["code"], redirectUri = "nucleus:rest" },
                        id = Guid.NewGuid()
                    });
    
                var tokenAuthResult = eaAuthClient.Execute<dynamic>(loginViaTokenRequest);
    
          var sessionId = tokenAuthResult.Data["result"]["id"];
    Now make your stats request.

    Code:
       var getCareerForPersonaRequest = new RestRequest("jsonrpc/web/api?Stats.getCareerForOwnedGamesByPersonaId", Method.POST);
    
                getCareerForPersonaRequest.AddHeader("X-GatewaySession", sessionId);
                getCareerForPersonaRequest.RequestFormat = DataFormat.Json;
                getCareerForPersonaRequest.AddBody(
                    new
                    {
                        jsonrpc = "2.0",
                        method = "Stats.getCareerForOwnedGamesByPersonaId",
                        @params = new {personaId = "180101517"},
                        id = Guid.NewGuid()
                    });
    
    
                var result = eaAuthClient.Execute(getCareerForPersonaRequest);
    Code is a little rough. Was just trying to develop a proof of concept.

  16. #16
    New User
    Join Date
    2016-10-28
    Posts
    3

    Re: Battlefield 1 API

    Hey nice work there sbauer318. I've been working on a python script to do this same thing.

    I can confirm the IDs are random. The relevant script is in the init.js

    Code:
            createUUID: function () {
                // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
                return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
                    var r = Math.random() * 16 | 0, v = c == 'x' ? r : r & 0x3 | 0x8;
                    return v.toString(16);
                });
            },
    Did you notice the OPTIONS requests before each POST? I'm guessing those aren't necessary, but I'm receiving a 400 status when trying to send my auth token and get my gateway id.

    Also, does anyone happen to know if there is a RPC call to look up persona IDs via name, or if something like that will ever be added? Right now I can do what I need with my friends list but that extra functionality would be nice.

  17. #17

    Re: Battlefield 1 API

    Quote Originally Posted by shade View Post
    Hey nice work there sbauer318. I've been working on a python script to do this same thing.

    I can confirm the IDs are random. The relevant script is in the init.js

    Code:
            createUUID: function () {
                // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
                return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
                    var r = Math.random() * 16 | 0, v = c == 'x' ? r : r & 0x3 | 0x8;
                    return v.toString(16);
                });
            },
    Did you notice the OPTIONS requests before each POST? I'm guessing those aren't necessary, but I'm receiving a 400 status when trying to send my auth token and get my gateway id.

    Also, does anyone happen to know if there is a RPC call to look up persona IDs via name, or if something like that will ever be added? Right now I can do what I need with my friends list but that extra functionality would be nice.
    Can you post your script?

    OPTIONS requests are for CORS requests. If you're executing the calls via javascript in a browser from a different hostname, it will send an OPTIONS request to determine if you're able to make the request.

  18. #18
    New User
    Join Date
    2016-10-28
    Posts
    3

    Re: Battlefield 1 API

    My script isn't even in a form I can run yet, just bits of the login process I'm collecting as I step through the process. I'd be happy to post once it works though.

    The OPTIONS requests I'm seeing are in chrome's devtools under XHR data. This is only from my PC, no where else. The only special data sent back and forth are in the headers.

    Code:
    #Before login request 
    Access-Control-Request-Headers:content-type
    Access-Control-Request-Method:POST
    
    #After login request 
    Access-Control-Request-Headers:content-type, x-gatewaysession
    Access-Control-Request-Method:POST
    
    #Always the response
    Access-Control-Allow-Headers:Content-Type, Accept, X-GatewaySession
    access-control-allow-methods:GET, POST, HEAD, OPTIONS

  19. #19
    New User
    Join Date
    2016-10-28
    Posts
    3

    Re: Battlefield 1 API

    Ok, the OPTIONS requests don't matter.
    Last edited by shade; 2016-10-29 at 08:17.

  20. #20

    Re: Battlefield 1 API

    Has anyone made any progress on this one with Python? Still clueless how this should be done...

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
P-STATS NETWORK FORUM LOGIN CREATE ACCOUNT