Skip to content

Commit 4c66c61

Browse files
authored
Fix GetTrophies method to use updated PSN API endpoint (#27)
* Fix GetTrophies method to use updated PSN API endpoint Updated the GetTrophies method to use the new PlayStation Network API endpoint (m.np.playstation.com/api/trophy/v1) instead of the deprecated regional endpoint. This fixes the HTTP 503 DNS failure errors reported in issue #24. Changes: - Updated API base URL from region-based endpoint to new unified endpoint - Changed URL pattern to include /users/ path and /npCommunicationIds/ - Added required npServiceName=trophy query parameter for PS3/PS4/Vita compatibility - Replaced custom headers with Accept-Language header for language selection - Removed region dependency from the trophy endpoint Fixes #24 * fix urls ---------
1 parent 1c09119 commit 4c66c61

File tree

4 files changed

+65
-47
lines changed

4 files changed

+65
-47
lines changed

README.md

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ Copy this js code:
4545
</details>
4646

4747
### Functionality
48-
- You can get user profile info
49-
- You can get trophy titles
50-
- You can get trophy groups
51-
- You can get trophies
48+
- Get user profile information
49+
- Get trophy titles for a user
50+
- Get trophy groups for a specific title
51+
- Get trophies for a specific title and group
5252

53-
### Example
53+
### Example
5454
```go
5555
package main
5656

@@ -86,15 +86,16 @@ func main() {
8686
// panic(err)
8787
// }
8888

89-
// Get user profile information.
90-
profile, err := client.GetProfile(ctx, "geeek_52rus")
89+
// Get user profile information by Online ID (username).
90+
profile, err := client.GetProfile(ctx, "your_psn_username")
9191
if err != nil {
9292
panic(err)
9393
}
9494
fmt.Printf("Profile: %+v\n", profile)
9595

96-
// Get trophy titles.
97-
trophyTitles, err := client.GetTrophyTitles(ctx, "geeek_52rus", 50, 0)
96+
// Get trophy titles for your account.
97+
// Use "me" for your own account, or a numeric accountId for other users.
98+
trophyTitles, err := client.GetTrophyTitles(ctx, "me", 50, 0)
9899
if err != nil {
99100
panic(err)
100101
}
@@ -103,20 +104,32 @@ func main() {
103104
// Get trophy groups for a specific title.
104105
if len(trophyTitles.TrophyTitles) > 0 {
105106
trophyTitleID := trophyTitles.TrophyTitles[0].NpCommunicationID
106-
trophyGroups, err := client.GetTrophyGroups(ctx, trophyTitleID, "geeek_52rus")
107+
trophyGroups, err := client.GetTrophyGroups(ctx, trophyTitleID, "me")
107108
if err != nil {
108109
panic(err)
109110
}
110111
fmt.Printf("Trophy Groups: %+v\n", trophyGroups)
111112
}
112113

113114
// Get trophies for a specific title and group.
114-
trophies, err := client.GetTrophies(ctx, "NPWR13348_00", "001", "geeek_52rus")
115+
// Parameters: npCommunicationId, trophyGroupId (e.g., "default" or "001"), accountId
116+
trophies, err := client.GetTrophies(ctx, "NPWR20188_00", "default", "me")
115117
if err != nil {
116118
panic(err)
117119
}
118120
fmt.Printf("Trophies: %+v\n", trophies)
119121
}
120122
```
123+
124+
### Important Notes
125+
126+
**API Changes (v2 API Migration):**
127+
- `GetTrophyTitles()` now requires an `accountId` parameter instead of `username`. Use `"me"` for your own account.
128+
- `GetTrophies()` now uses the updated PSN API endpoint (`m.np.playstation.com/api/trophy/v1`).
129+
- The API no longer uses region-specific endpoints for trophy operations.
130+
131+
**Account ID vs Online ID:**
132+
- Some methods accept `onlineId` (username like "VaultTec_Trading")
133+
- Trophy-related methods require `accountId` (use `"me"` for your account or a numeric account ID for others)
121134
This project highly inspired by https://github.com/Tustin/psn-php. Some useful things like auth headers and params found in `Tustin/psn-php`.
122135
<p align="center"> <img src="assets/gopher-dance.gif"> </p>

trophy.go

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
)
77

8-
const trophiesApi = "-tpy.np.community.playstation.net/trophy/v1/trophyTitles/"
8+
const trophiesApi = "m.np.playstation.com/api/trophy/v1/users/"
99

1010
type TrophiesResponse struct {
1111
Trophies []struct {
@@ -26,29 +26,38 @@ type TrophiesResponse struct {
2626
}
2727

2828
// GetTrophies retrieves a user's trophies for a specific title and group.
29-
func (c *Client) GetTrophies(ctx context.Context, trophyTitleId, trophyGroupId, username string) (*TrophiesResponse, error) {
29+
// accountId: Use "me" for the authenticating account or a numeric accountId to query another user's trophies.
30+
// trophyGroupId: Use "all" to get all trophies, "default" for base game, or specific group IDs like "001", "002", etc.
31+
// Note: For PS5/PC games, no npServiceName is needed. For PS3/PS4/Vita games, use GetTrophiesWithServiceName with npServiceName="trophy".
32+
func (c *Client) GetTrophies(ctx context.Context, trophyTitleId, trophyGroupId, accountId string) (*TrophiesResponse, error) {
33+
return c.GetTrophiesWithServiceName(ctx, trophyTitleId, trophyGroupId, accountId, "")
34+
}
35+
36+
// GetTrophiesWithServiceName retrieves a user's trophies for a specific title and group with an explicit npServiceName.
37+
// accountId: Use "me" for the authenticating account or a numeric accountId to query another user's trophies.
38+
// trophyGroupId: Use "all" to get all trophies, "default" for base game, or specific group IDs like "001", "002", etc.
39+
// npServiceName: Use "trophy" for PS3/PS4/Vita, "trophy2" for PS5/PC, or "" (empty) to omit the parameter.
40+
func (c *Client) GetTrophiesWithServiceName(ctx context.Context, trophyTitleId, trophyGroupId, accountId, npServiceName string) (*TrophiesResponse, error) {
3041
var h = headers{}
3142
h["authorization"] = fmt.Sprintf("Bearer %s", c.accessToken)
32-
h["Accept"] = "*/*"
33-
h["Accept-Encoding"] = "gzip, deflate, br"
43+
h["Accept-Language"] = c.lang
3444

3545
var trophiesResponse TrophiesResponse
36-
err := c.get(
37-
ctx,
38-
fmt.Sprintf(
39-
"https://%s%s%s/trophyGroups/%s/trophies?fields=@default,trophyRare,trophyEarnedRate,trophySmallIconUrl&visibleType=1&comparedUser=%s&npLanguage=%s",
40-
c.region,
41-
trophiesApi,
42-
trophyTitleId,
43-
trophyGroupId,
44-
username,
45-
c.lang,
46-
),
47-
h,
48-
&trophiesResponse,
46+
url := fmt.Sprintf(
47+
"https://%s%s/npCommunicationIds/%s/trophyGroups/%s/trophies",
48+
trophiesApi,
49+
accountId,
50+
trophyTitleId,
51+
trophyGroupId,
4952
)
53+
54+
if npServiceName != "" {
55+
url += "?npServiceName=" + npServiceName
56+
}
57+
58+
err := c.get(ctx, url, h, &trophiesResponse)
5059
if err != nil {
5160
return nil, fmt.Errorf("failed to get trophies: %w", err)
5261
}
5362
return &trophiesResponse, nil
54-
}
63+
}

trophy_group.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"time"
77
)
88

9-
const trophyGroupApi = "-tpy.np.community.playstation.net/trophy/v1/trophyTitles/"
9+
const trophyGroupApi = "m.np.playstation.com/api/trophy/v1/users/"
1010

1111
type TrophyGroupResponse struct {
1212
TrophyTitleName string `json:"trophyTitleName"`
@@ -46,22 +46,20 @@ type TrophyGroupResponse struct {
4646
}
4747

4848
// GetTrophyGroups retrieves a user's trophy groups for a specific title.
49-
func (c *Client) GetTrophyGroups(ctx context.Context, trophyTitleId, username string) (*TrophyGroupResponse, error) {
49+
// accountId: Use "me" for the authenticating account or a numeric accountId to query another user's trophy groups.
50+
func (c *Client) GetTrophyGroups(ctx context.Context, trophyTitleId, accountId string) (*TrophyGroupResponse, error) {
5051
var h = headers{}
5152
h["authorization"] = fmt.Sprintf("Bearer %s", c.accessToken)
52-
h["Accept"] = "*/*"
53-
h["Accept-Encoding"] = "gzip, deflate, br"
53+
h["Accept-Language"] = c.lang
5454

5555
var response TrophyGroupResponse
5656
err := c.get(
5757
ctx,
5858
fmt.Sprintf(
59-
"https://%s%s%s/trophyGroups?fields=@default,trophyGroupSmallIconUrl&comparedUser=%s&npLanguage=%s",
60-
c.region,
59+
"https://%s%s/npCommunicationIds/%s/trophyGroups",
6160
trophyGroupApi,
61+
accountId,
6262
trophyTitleId,
63-
username,
64-
c.lang,
6563
),
6664
h,
6765
&response,
@@ -70,4 +68,4 @@ func (c *Client) GetTrophyGroups(ctx context.Context, trophyTitleId, username st
7068
return nil, fmt.Errorf("failed to get trophy groups: %w", err)
7169
}
7270
return &response, nil
73-
}
71+
}

trophy_title.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"time"
77
)
88

9-
const trophyTitleApi = "-tpy.np.community.playstation.net/trophy/v1/trophyTitles?"
9+
const trophyTitleApi = "m.np.playstation.com/api/trophy/v1/users/"
1010

1111
type TrophyTitleResponse struct {
1212
TotalResults int `json:"totalResults"`
@@ -53,23 +53,21 @@ type TrophyTitleResponse struct {
5353
}
5454

5555
// GetTrophyTitles retrieves a user's trophy titles.
56-
func (c *Client) GetTrophyTitles(ctx context.Context, username string, limit, offset int) (*TrophyTitleResponse, error) {
56+
// accountId: Use "me" for the authenticating account or a numeric accountId to query another user's titles.
57+
func (c *Client) GetTrophyTitles(ctx context.Context, accountId string, limit, offset int) (*TrophyTitleResponse, error) {
5758
var h = headers{}
5859
h["authorization"] = fmt.Sprintf("Bearer %s", c.accessToken)
59-
h["Accept"] = "*/*"
60-
h["Accept-Encoding"] = "gzip, deflate, br"
60+
h["Accept-Language"] = c.lang
6161

6262
var response TrophyTitleResponse
6363
err := c.get(
6464
ctx,
6565
fmt.Sprintf(
66-
"https://%s%sfields=@default,trophyTitleSmallIconUrl&platform=PS3,PS4,PSVITA&limit=%d&offset=%d&comparedUser=%s&npLanguage=%s",
67-
c.region,
66+
"https://%s%s/trophyTitles?limit=%d&offset=%d",
6867
trophyTitleApi,
68+
accountId,
6969
limit,
7070
offset,
71-
username,
72-
c.lang,
7371
),
7472
h,
7573
&response,
@@ -78,4 +76,4 @@ func (c *Client) GetTrophyTitles(ctx context.Context, username string, limit, of
7876
return nil, fmt.Errorf("failed to get trophy titles: %w", err)
7977
}
8078
return &response, nil
81-
}
79+
}

0 commit comments

Comments
 (0)