andshrew / PlayStation-Trophies

Sony has an API for retrieving details of the trophies an account has earned, but there is no public documentation for using it. This is an attempt at documenting the API by capturing the requests made by the https://my.playstation.com web site.
MIT License
100 stars 4 forks source link

How to get the needed "npCommunicationId" from CUSA and PPSA? #5

Closed MedusaLuchs85 closed 2 years ago

MedusaLuchs85 commented 3 years ago

Hello all,

since the API has been revised by Sony, I need some help to get special game information. For PS4 games I can still use this API call to get the npCommunicationid which is needed for e.g. the trophy list of the game:

CUSA02387_00 --> Battlefield 1

https://us-tpy.np.community.playstation.net/trophy/v1/apps/trophyTitles?fields=@default&npLanguage=de&npTitleIds=CUSA02387_00

A PS5 game like PPSA01349 will not work to get the needed NPWRxxxxx_00 id with this API call.

The above call will return the npCommunicationId: NPWR08899_00 only for PS4 games.

This call get all trophy sets of a game in general without user account binding.

https://m.np.playstation.net/api/trophy/v1/npCommunicationIds/NPWR08899_00/trophyGroups/all/trophies?npServiceName=trophy

For every request the AccessToken is needed!!!! To get this token, see the documentation here.

My question ist now, how to get the npCommunitionId (NPWRxxxxx_00) for every title (Ps4,Ps5), independent of the user account. Just to get a preview of the game trophies before purchasing the game.

Hopefully someone has an idea or a APi call to get the NPWRxxxxx_00 id for every platform.

Best regards

andshrew commented 3 years ago

Hi @MedusaLuchs85

The only endpoint which I am aware of that will give you the npCommunicationId (NPRW...) for a PS5 title ID makes the query against a specific PSN account; so if that account has not played the title then you won't get a result back.

If what you want to do is extract a full trophy list from every trophy set then the only way I know to do that would be to recursively brute force the API by iterating through every possible NPRW ID and saving the results.

You can do that with the Title Trophy Groups endpoint. That will confirm ID exists and give you the name of the trophy set.

So you would start at NPWR00001_00 and work your way up to a reasonable number, maybe around NPWR25000_00.

Once you have all of the valid IDs then you could make a request to retrieve the individual trophies for a title.

But that's a lot of effort if you just want to see the trophy list for a game before buying it; there are loads of trophy web sites like https://www.playstationtrophies.org/games/ps5/ that have this information easily accessible.

MedusaLuchs85 commented 3 years ago

Hi @andshrew

You are absolutely right. Currently, this seems to be the only way to get this information. Either you have the game installed or you have to check each number individually. But that would be messy, and SONY also has a limit on the number of requests. Various platforms such as PSNProfiles have this information, but of course no one knows exactly how this data is gathered. Maybe in the future there will be an API (like currently for PS4 games) that will work for all game titles.

Maybe there is just one missing parameter in the PS4 API endpoint, I don't know. This API is not public, so it's hard to say anything about it. I even tried to go through the store and get the NPWR via the ConceptId. But unfortunately without success. The determination of this NPWR is apparently a well-kept secret.

Thanks anyway, if you find a way, please share it with us.

GarrettAlbright commented 3 years ago

The only endpoint which I am aware of that will give you the npCommunicationId (NPRW...) for a PS5 title ID makes the query against a specific PSN account; so if that account has not played the title then you won't get a result back.

I might be taking this approach in the project I'm working on, but the next question becomes: How can one find the account ID for someone's/their own account? This could be via an API or via a human-oriented web page. Judging from this documentation it's a fairly long base10 number but I don't see anything like that after logging in to my test Sony account.

Edit: It looks like I can get it for the "current" account by using the "trophySummary" endpoint with "me" as the account ID, which will return "my" numeric account ID. So it looks like I could have someone log in and get their NPSSO as per the instructions in these docs and then use that to log in as them and get their account ID. Is there an easier way that doesn't require someone to basically turn their account over to me though?

andshrew commented 3 years ago

Hi @GarrettAlbright

Yes 'me' can be used in many instances to represent the authenticating account so you don't need to know your own ID. Have a look at these as a starting point for finding others. These all use the same authentication token which I've documented here for the trophies.

https://m.np.playstation.net/api/userProfile/v1/internal/users/me/friends

This returns the authenticating accounts friends list in account ID format. Replace "me" with another accounts ID to see their friends list from the perspective of the authenticating account (so what you see would depend on their privacy settings).

https://m.np.playstation.net/api/userProfile/v1/internal/users/profiles?accountIds=123456789

This returns a summary of the account IDs you submit (you can comma separate them to query more than one at a time for example ?accountIds=123456789%2C123456). If you do query more than one at a time the actual account IDs are not returned in the response but the order they are returned in matches the order you submit them in the request.

Lastly if you just want to find your own account ID then it's returned in a request which the PS Store makes; look for these requests in your browser developer tools when logged in to the PS Store:

https://web.np.playstation.com/api/graphql/v1/op?operationName=getProfileOracle

GarrettAlbright commented 3 years ago

Lastly if you just want to find your own account ID then it's returned in a request which the PS Store makes; look for these requests in your browser developer tools when logged in to the PS Store:

Ah, nice. This should do the job for our needs. Thank you!

XenorPLxx commented 2 years ago

The only endpoint which I am aware of that will give you the npCommunicationId (NPRW...) for a PS5 title ID makes the query against a specific PSN account; so if that account has not played the title then you won't get a result back.

@andshrew could you share a link to the endpoint you had in mind?

andshrew commented 2 years ago

Hi @XenorPLxx

Yes, it's https://m.np.playstation.net/api/trophy/v1/users/me/titles/trophyTitles?npTitleIds=PPSA01284_00

You'll get a response like:

{
  "titles": [
    {
      "npTitleId": "PPSA01284_00",
      "trophyTitles": [
        {
          "npServiceName": "trophy2",
          "npCommunicationId": "NPWR20004_00",
          "trophyTitleName": "Returnal",
          "trophyTitleIconUrl": "https://psnobj.prod.dl.playstation.net/psnobj/NPWR20004_00/f60fd55f-a01f-4274-a865-d8356dc0fd9c.png",
          "hasTrophyGroups": false,
          "rarestTrophies": [],
          "progress": 0,
          "earnedTrophies": {
            "bronze": 0,
            "silver": 0,
            "gold": 0,
            "platinum": 0
          },
          "definedTrophies": {
            "bronze": 18,
            "silver": 5,
            "gold": 7,
            "platinum": 1
          },
          "lastUpdatedDateTime": "2021-05-04T12:54:39Z"
        }
      ]
    }
  ]
}

As with other endpoints "me" is the authenticating account but this can be substituted for the account ID of another account. You'll only get a response if the account you're querying against has played the game.

You can query multiple title IDs at once by comma separating them in the request, for example https://m.np.playstation.net/api/trophy/v1/users/me/titles/trophyTitles?npTitleIds=CUSA10468_00%2CPPSA01284_00

andshrew commented 2 years ago

This has been incorporated into the main documentation now:

https://github.com/andshrew/PlayStation-Trophies/commit/75412f3246a75e3e342a63d034cebaee8e2e5d94