achievements-app/psn-api

Unable to retrieve psn access code.

darias08 opened this issue · 12 comments

Hello, I am having some trouble getting all my game titles to show on my console.log. I am getting an error message saying that there was a problem retrieving my psn access code. Saying that is my NPSSO code valid? I have placed my Npsso code from the link provided to get my npsso and it's saying that it didn't retrieve it. I have tried resetting my browser history and using a different Npsso id and it still gives me the same error.

Here's what I have inside my code:

import { getUserTitles, makeUniversalSearch, exchangeCodeForAccessToken, exchangeNpssoForCode } from 'psn-api';

async function connectPSProfile() {

  // This is the value you copied from the previous step.
 const myNpsso = <This is where I place my Npsso code> 

  // We'll exchange your NPSSO for a special access code.
    const accessCode = await exchangeNpssoForCode(myNpsso);
  
   // We can use the access code to get your access token and refresh token.
    const authorization = await exchangeCodeForAccessToken(accessCode);
  
   //Finding the account
    const allAccountsSearchResults = await makeUniversalSearch(
      authorization, 
      "me", 
      "SocialAllAcounts"
    );
  
    //Finding the account id
    const targetAccountId = 
    allAccountsSearchResults.domainResponses[0].results[0].socialMetadata.accountId;
  
    //Getting the list of games
    const titles = await getUserTitles(authorization, targetAccountId);
  
    console.log(titles); 

}

All of this is placed in my Playstation.js file. I am not sure if I am supposed to be using typescript or did I put it in the incorrect location? Please let me know what's causing this issue. Thank you!

Here's the error message of what is saying:
WARN Possible Unhandled Promise Rejection (id: 3):
Error:
There was a problem retrieving your PSN access code. Is your NPSSO code valid?
To get a new NPSSO code, visit https://ca.account.sony.com/api/v1/ssocookie.

Hello, Just to further this post. I got the access code to work However, it is asking for the "URLSearchParams.get" Do you know where I can get the URL to access my profile data and how would I implement that in my code?

Hi!

Can you share an exact stack trace for the error you are seeing?

Hello!

So I am very new to using API just to give you a heads up lol, but here's a screenshot of what I am getting from the error.

https://drive.google.com/file/d/1w4xMcTghkmKwFf0c-G6g5NZSxgi9N55e/view?usp=sharing

I crossed out things in red that hold my Http://localhost:8081 which contains personal information. So please let me know if I have provided you with the right information. I am just trying to read my psn game titles through the console log :(

Please let me know if you need a copy of my .js file that I am invoking the psn-api

Hi @darias08 , no worries. I have spent a little bit of time today hunting this down.

Your screenshot provided a very helpful clue. You may be the first person to have used psn-api with React Native :-)
I found this thread during my search: facebook/react-native#23922

I suspect that URLSearchParams, which is a native JS API that psn-api depends on to help keep the bundle size low, is not properly implemented in a react-native context. This is what is causing the .get() function calls to fail. psn-api is hitting a line of code that works in modern browsers, but not in React Native. I believe there is nothing wrong with your code -- if you executed it in a web browser I am quite sure it would just work.

My experience with React Native is limited, but I found this package which polyfills in support for the API: https://github.com/charpeni/react-native-url-polyfill

I am wondering if you went with option 1 in this package's README, which imports the whole polyfill, if this issue would go away. I think as long as the polyfill is loaded before any psn-api code is loaded, the problem should be solved.

I'm not sure how React Native handles CORS, but assuming the polyfill works there's a probability you'll bump into a CORS issue from the PlayStation Network itself next. If that happens report back and we can work through that too.

Hello!

Thank you so much for these awesome resources! :D So I went with option 1 since it seems the most simple way to do it. So the good news is that the URLSearchParams.get() is no longer showing the error, but it's saying that the socialMetadata is undefined.

Here's the error I am receiving:

( WARN Possible Unhandled Promise Rejection (id: 0):
TypeError: Cannot read property 'socialMetadata' of undefined
TypeError: Cannot read property 'socialMetadata' of undefined)

Here's a screenshot link of what I displayed on my code. Let me know if you have any suggestions!

https://drive.google.com/file/d/13nEXLDSd9ZhWWyqFqQ_SYokeDKtJlPNy/view?usp=share_link

Hey @darias08

Interesting -- that error is appearing because the results array is empty.

I would console.log(allAccountsSearchResults). From there, I think there are two possible outcomes:

1- You will see a similar unhandled promise rejection error. This means there is likely an issue with connection to PSN (probably CORS).

2- You will see an empty or near-empty object. This means connection is fine and something is wrong with the search query itself.

Issue 1 is resolvable. Issue 2 is bit trickier.

I suspect what you're encountering is issue 1. If that's the case, the PlayStation Network is actively blocking the API call from your app for security purposes. This has come up a few times in other issues filed in this repo - PSN blocks client-side API calls, but those rules are actually only enforced by the web browser and I'm unsure if they apply to React Native.

If this does turn out to be a CORS issue, the solution is to move your psn-api code to a custom Node.js endpoint, host it somewhere, then have your React Native app call that endpoint. This will make PSN happy and the code will just work (you can probably test this out in a small Node.js app to confirm before building a server/endpoint for it).

I believe the reason this rule exists is they don't want an NPSSO winding up in a client-side bundle. I can only speculate that's the reason though.

Hi again!

So, I want to know more about this api and how it works. First, let me explain what I am planning to do with this api since I didn't quite explain why I am using this. So I am trying to have a user type in their user or (PSN username) in a text input and once they hit connect it will search for that id in the psn database. I want to know how does myNpsso work? Is it just to gain access to the psn api and only have access within that account? Or can I have it set to search for other users and get their profile games? Like if I want to type in my username and have it search with this api and get a list of games. Could that work?

Hey @darias08

I want to know how does myNpsso work? Is it just to gain access to the psn api and only have access within that account?

The NPSSO is a special token given by Sony that authorizes someone to use their endpoints. This prevents bots and spammers from flooding the network with calls, and also gives Sony the ability to trace who exactly is making calls on the network.

Or can I have it set to search for other users and get their profile games?

Yep, depending on that user's privacy setting, you are able to get all their gameplay history, etc. This is how PSNProfiles works (and is why they require certain privacy settings to scrape someone's data properly).

Okay, so I am trying to test this out on Postman to get a visual understanding of the api call. So here's the thing, what would the search url be? Would I use the https://www.playstation.com ? and then the headers myNpsso as the access token? I am trying to display my list of games through postman.

Or is this not compatible with postman?

Incompatible :-)
The purpose of this library is to save someone from having to call the endpoints directly (effectively what Postman is doing). If you are interested in calling PSN directly, I would browse through the code to understand what psn-api is doing. You will find all the relevant calls/headers/params etc.

Happy to keep chatting! But I am going to close this issue for now. Feel free to keep responding though. If there is a bug in psn-api itself we can reopen.