JavaScript is required to use Bungie.net

Group Avatar

BungieNetPlatform

"Updates, discussions, and documentation of the BungieNetPlatform API."

Request Join
originally posted in:BungieNetPlatform
3/31/2015 8:59:53 PM
2

PSN Auth with Python/Requests

I haven't seen any Python specific implementations so I thought I'd share. The only requirement is [url=http://docs.python-requests.org/en/latest/]requests[/url]. If you don't yet have it I highly recommend it. https://gist.github.com/ascendancyy/702db99b626d52d69359 The two links we need are: BUNGIE_SIGNIN_URI = "https://www.bungie.net/en/User/SignIn/Psnid" PSN_OAUTH_URI = "https://auth.api.sonyentertainmentnetwork.com/login.do" 1. GET BUNGIE_SIGNIN_URI (I partly did this because I plan on adding support for Xbox sign-in). Since we allow redirections and we're not using a session we have to look in the history to find the cookie we want. This gets us our first JSESSIONID cookie from PSN. 2. POST PSN_OAUTH_URI We POST with a form-encoded body containing our username/password. We also pass in the JSESSIONID cookie from the first step. This gets us a new JSESSIONID cookie. In the header we'll find a location field. 3. GET the location field from the previous step. Make sure the pass in the JSESSIONID cookie from the second step. In the response header you'll see a field called[i] x-np-grant-code[/i]. 4. GET BUNGIE_SIGNIN_URI with the x-np-grant-code appended as a query string with [i]code[/i] as the field name. It should look something like: https://www.bungie.net/en/User/SignIn/Psnid?code=XXXXXX 5. We create a requests Session. We save the [i]bungled[/i] and [i]bungleatk[/i] cookies to our session. We also add our API key to our headers and add the bungled cookie to the [i]x-csrf[/i] field. Now all future requests from our Session will work. At first I tried doing this over a single session, but it wouldn't work - at least not the private APIs. Only once did I use individual requests did it work. It's most likely due to some cookie issues, but I can't really be bothered to figure out exactly why. Cheers.

Posting in language:

 

Play nice. Take a minute to review our Code of Conduct before submitting your post. Cancel Edit Create Fireteam Post

  • Could you post an example of a request using the headers after the authentication? I'm sending a request using the following code after the code you posted above and still getting an authentication error: stuff = requests.get("http://www.bungie.net/Platform/Destiny/2/MyAccount/Character/2305843009217406736/Advisors/", headers=session.headers) Note: I moved the session initialization outside of your login method to make the headers accessible outside the method. Should I just make my requests inside of your method as well? Thanks in advance if you get a chance to answer!

    Posting in language:

     

    Play nice. Take a minute to review our Code of Conduct before submitting your post. Cancel Edit Create Fireteam Post

    2 Replies
    • Thanks so much for this! Been messing with this to no avail, you've saved me hours of frustration.

      Posting in language:

       

      Play nice. Take a minute to review our Code of Conduct before submitting your post. Cancel Edit Create Fireteam Post

    You are not allowed to view this content.
    ;
    preload icon
    preload icon
    preload icon