Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using API on a PHP project #31

Open
Askancy opened this issue Nov 19, 2024 · 2 comments
Open

Using API on a PHP project #31

Askancy opened this issue Nov 19, 2024 · 2 comments

Comments

@Askancy
Copy link

Askancy commented Nov 19, 2024

I am trying to use the API in a laravel php project, however I am having problems connecting, this is my code:

        $npsso = "*****"; 
        
            $authParams = http_build_query([
                'access_type' => 'offline',
                'client_id' => '09515159-7237-4370-9b40-3806e67c0891',
                'response_type' => 'code',
                'scope' => 'psn:mobile.v2.core psn:clientapp',
                'redirect_uri' => 'com.scee.psxandroid.scecompcall://redirect',
            ]);
        
            $authUrl = "https://ca.account.sony.com/api/authz/v3/oauth/authorize?$authParams";
        
            try {
                $authResponse = Http::withHeaders([
                    'Cookie' => "npsso=$npsso",
                ])->get($authUrl);
        
                $location = $authResponse->header('Location');
        
                if (!$location || !str_contains($location, 'code=')) {
                    return response()->json(['error' => 'Authorization code not found.'], 400);
                }
        
                parse_str(parse_url($location, PHP_URL_QUERY), $query);
                $code = $query['code'] ?? null;
        
                if (!$code) {
                    return response()->json(['error' => 'Failed to extract authorization code.'], 400);
                }
        
                $tokenResponse = Http::asForm()->post('https://ca.account.sony.com/api/authz/v3/oauth/token', [
                    'code' => $code,
                    'grant_type' => 'authorization_code',
                    'token_format' => 'jwt',
                ]);
        
                if ($tokenResponse->failed()) {
                    return response()->json([
                        'error' => 'Failed to exchange authorization code for token.',
                        'details' => $tokenResponse->body(),
                    ], $tokenResponse->status());
                }
        
                $accessToken = $tokenResponse->json()['access_token'] ?? null;
        
                if (!$accessToken) {
                    return response()->json(['error' => 'Access token not found.'], 400);
                }
        
                $trophyUrl = "https://m.np.playstation.com/api/trophy/v1/npCommunicationIds/NPWR10600_00/trophyGroups/all/trophies?npServiceName=trophy";
        
                $trophyResponse = Http::withHeaders([
                    'Authorization' => "Bearer $accessToken",
                ])->get($trophyUrl);
        
                if ($trophyResponse->failed()) {
                    return response()->json([
                        'error' => 'Failed to fetch trophies.',
                        'status' => $trophyResponse->status(),
                        'message' => $trophyResponse->body(),
                    ], $trophyResponse->status());
                }
       
                return response()->json([
                    'trophies' => $trophyResponse->json(),
                ]);
        
            } catch (\Exception $e) {
                return response()->json(['error' => $e->getMessage()], 500);
            }

But I get the error that the redirect is not a compatible protocol... how can I solve this? Has anyone already implemented this API on a php/laravel project?

{"error":"Redirect URI, com.scee.psxandroid.scecompcall://redirect/?code=v3.jGU8m6&cid=b52b4b25-a053-493d-b828-aaf5e80f7e68, does not use one of the allowed redirect protocols: http, https"}

@Ragowit
Copy link

Ragowit commented Nov 19, 2024

PSN-PHP, https://github.com/tustin/psn-php/

@andshrew
Copy link
Owner

Hi @Askancy

I'm not familiar with PHP, but I'm guessing something like parse_url is expecting http or https but you've giving it com.scee.psxandroid.scecompcall. Simple suggestion would be to just substitute that for either http or https in the response before trying to parse it.

But otherwise as @Ragowit suggested, have a look at psn-php for a ready to use example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants