psn_module

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2025 License: MIT Imports: 7 Imported by: 0

README

Go Reference build status build status

go-psn-api

A Playstation Network API wrapper written in Go.

Read first

Corresponding to my research how PSN works you need npsso to interact with Sony servers. Instructions how to get it below.

How to get npsso

Fully described here - https://tusticles.com/psn-php/first_login.html

If link above doesn't work

Copy this js code:

(function(open) {
    XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {

        this.addEventListener("readystatechange", function() {
            if (this.readyState == XMLHttpRequest.DONE) {
                let response = JSON.parse(this.responseText);

                if (response && "npsso" in response) {
                    console.log('found npsso', response.npsso);
                }
            }
        }, false);

        open.call(this, method, url, async, user, pass);
    };

    window.onbeforeunload = function(){
        return 'Are you sure you want to leave?';
    };

})(XMLHttpRequest.prototype.open);
  • Navigate to https://account.sonyentertainmentnetwork.com/ in your browser and open your browser’s developer console
  • Paste the above Javascript into the console and then login.
  • After the login flow is completed, you should see a new log in the developer console that looks like: found npsso <64 character code>. Copy that 64 character code.
Functionality
  • You can get user profile info
  • You can get trophy titles
  • You can get trophy groups
  • You can get trophies
Example
package main

import (
  "fmt"
  "github.com/sizovilya/go-psn-api"
)

func main() {
  ctx := context.Background()
  lang := "ru" // known list here https://github.com/sizovilya/go-psn-api/blob/main/langs.go, some languages in list are wrong and unsupported now, feel free to investigate for your own and add it to list
  region := "ru" // known list here https://github.com/sizovilya/go-psn-api/blob/main/regions.go, some regions in list are wrong and unsupported now, feel free to investigate for your own and add it to list
  npsso := "<your npsso>"
  psnApi, err := psn.NewPsnApi(
    lang,
    region,
  )
  if err != nil {
    panic(err)
  }

  // This request will get access and refresh tokens from Sony's servers
  err = psnApi.AuthWithNPSSO(ctx, npsso)
  if err != nil {
    panic(err)
  }

  // If you obtain refresh token you may use it for next logins.
  // Next logins should be like this:
  // refreshToken, _ := psnApi.GetRefreshToken() // store refresh token somewhere for future logins by psnApi.AuthWithRefreshToken method
  err = psnApi.AuthWithRefreshToken(ctx, "<your token>") // will get new access token, feel free to manage tokens by yourself
  if err != nil {
    panic(err)
  }

  // How to get user's profile info
  profile, err := psnApi.GetProfileRequest(ctx, "geeek_52rus")
  if err != nil {
    panic(err)
  }
  fmt.Print(profile)

  // How to get trophy titles
  trophyTitles, err := psnApi.GetTrophyTitles(ctx, "geeek_52rus", 50, 0)
  if err != nil {
    panic(err)
  }
  fmt.Print(trophyTitles)

  // How to get trophy group by trophy title
  trophyTitleId := trophyTitles.TrophyTitles[0].NpCommunicationID // get first of them
  trophyGroups, err := psnApi.GetTrophyGroups(ctx, trophyTitleId, "geeek_52rus")
  if err != nil {
    panic(err)
  }
  fmt.Println(trophyGroups)

  // How to get trophies in certain trophy title and trophy group
  trophies, err := psnApi.GetTrophies(
  	ctx,
	"NPWR13348_00", // The Last of Us 2
	"001",         // trophy group with id = 001
	"geeek_52rus",
  )
  if err != nil {
      panic(err)
  }
  fmt.Println(trophies)
}

This project highly inspired by https://github.com/Tustin/psn-php. Some useful things like auth headers and params found in Tustin/psn-php.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AvatarUrls

type AvatarUrls struct {
	Size      string `json:"size"`
	AvatarURL string `json:"avatarUrl"`
}

type ConsoleAvailability

type ConsoleAvailability struct {
	AvailabilityStatus string `json:"availabilityStatus"`
}

type EarnedTrophies

type EarnedTrophies struct {
	Platinum int `json:"platinum"`
	Gold     int `json:"gold"`
	Silver   int `json:"silver"`
	Bronze   int `json:"bronze"`
}

type PersonalDetail

type PersonalDetail struct {
	FirstName string `json:"firstName"`
	LastName  string `json:"lastName"`
}

type Presences

type Presences struct {
}

type Profile

type Profile struct {
	OnlineID              string              `json:"onlineId"`
	NpID                  string              `json:"npId"`
	AvatarUrls            []AvatarUrls        `json:"avatarUrls"`
	Plus                  int                 `json:"plus"`
	AboutMe               string              `json:"aboutMe"`
	LanguagesUsed         []string            `json:"languagesUsed"`
	TrophySummary         TrophySummary       `json:"trophySummary"`
	IsOfficiallyVerified  bool                `json:"isOfficiallyVerified"`
	PersonalDetail        PersonalDetail      `json:"personalDetail"`
	PersonalDetailSharing string              `json:"personalDetailSharing"`
	PrimaryOnlineStatus   string              `json:"primaryOnlineStatus"`
	Presences             []Presences         `json:"presences"`
	FriendRelation        string              `json:"friendRelation"`
	Blocking              bool                `json:"blocking"`
	MutualFriendsCount    int                 `json:"mutualFriendsCount"`
	Following             bool                `json:"following"`
	FollowerCount         int                 `json:"followerCount"`
	ConsoleAvailability   ConsoleAvailability `json:"consoleAvailability"`
}

type ProfileResponse

type ProfileResponse struct {
	Profile Profile `json:"profile"`
}

type Psn added in v1.0.4

type Psn struct {
	// contains filtered or unexported fields
}

func NewPsnApi

func NewPsnApi(lang, region string) (*Psn, error)

Creates new psn api

func (*Psn) AuthWithNPSSO added in v1.0.4

func (p *Psn) AuthWithNPSSO(ctx context.Context, npsso string) error

Method makes auth request to Sony's server and retrieves tokens

func (*Psn) AuthWithRefreshToken added in v1.0.4

func (p *Psn) AuthWithRefreshToken(ctx context.Context, refreshToken string) error

Method makes auth request to Sony's server and retrieves tokens

func (*Psn) GetAccessToken added in v1.0.4

func (p *Psn) GetAccessToken() (string, int32)

Getter for access token

func (*Psn) GetLang added in v1.0.4

func (p *Psn) GetLang() string

Getter for lang

func (*Psn) GetNPSSO added in v1.0.4

func (p *Psn) GetNPSSO() string

Getter for npsso

func (*Psn) GetProfileRequest added in v1.0.4

func (p *Psn) GetProfileRequest(ctx context.Context, name string) (profile *Profile, err error)

Method retrieves user profile info by PSN id

func (*Psn) GetRefreshToken added in v1.0.4

func (p *Psn) GetRefreshToken() (string, int32)

Getter for refresh token

func (*Psn) GetRegion added in v1.0.4

func (p *Psn) GetRegion() string

Getter for region

func (*Psn) GetTrophies added in v1.0.4

func (p *Psn) GetTrophies(ctx context.Context, trophyTitleId, trophyGroupId, username string) (*TrophiesResponse, error)

Method retrieves user's trophies

func (*Psn) GetTrophyGroups added in v1.0.4

func (p *Psn) GetTrophyGroups(ctx context.Context, trophyTitleId, username string) (*TrophyGroupResponse, error)

Method retrieves user's trophy groups

func (*Psn) GetTrophyTitles added in v1.0.4

func (p *Psn) GetTrophyTitles(ctx context.Context, username string, limit, offset int32) (*TrophyTitleResponse, error)

Method retrieves user's trophy titles

func (*Psn) SetAccessToken added in v1.0.4

func (p *Psn) SetAccessToken(accessToken string) error

Setter for access token

func (*Psn) SetLang added in v1.0.4

func (p *Psn) SetLang(lang string) error

Setter for lang

func (*Psn) SetNPSSO added in v1.0.4

func (p *Psn) SetNPSSO(npsso string) error

Setter for npsso

func (*Psn) SetRefreshToken added in v1.0.4

func (p *Psn) SetRefreshToken(refreshToken string) error

Getter for refresh token

func (*Psn) SetRegion added in v1.0.4

func (p *Psn) SetRegion(region string) error

Setter for region

type TrophiesResponse

type TrophiesResponse struct {
	Trophies []struct {
		TrophyID           int    `json:"trophyId"`
		TrophyHidden       bool   `json:"trophyHidden"`
		TrophyType         string `json:"trophyType"`
		TrophyName         string `json:"trophyName"`
		TrophyDetail       string `json:"trophyDetail"`
		TrophyIconURL      string `json:"trophyIconUrl"`
		TrophySmallIconURL string `json:"trophySmallIconUrl"`
		TrophyRare         int    `json:"trophyRare"`
		TrophyEarnedRate   string `json:"trophyEarnedRate"`
		FromUser           struct {
			OnlineID string `json:"onlineId"`
			Earned   bool   `json:"earned"`
		} `json:"fromUser,omitempty"`
	} `json:"trophies"`
}

type TrophyGroupResponse

type TrophyGroupResponse struct {
	TrophyTitleName     string `json:"trophyTitleName"`
	TrophyTitleDetail   string `json:"trophyTitleDetail"`
	TrophyTitleIconURL  string `json:"trophyTitleIconUrl"`
	TrophyTitlePlatfrom string `json:"trophyTitlePlatfrom"`
	DefinedTrophies     struct {
		Bronze   int `json:"bronze"`
		Silver   int `json:"silver"`
		Gold     int `json:"gold"`
		Platinum int `json:"platinum"`
	} `json:"definedTrophies"`
	TrophyGroups []struct {
		TrophyGroupID           string `json:"trophyGroupId"`
		TrophyGroupName         string `json:"trophyGroupName"`
		TrophyGroupDetail       string `json:"trophyGroupDetail"`
		TrophyGroupIconURL      string `json:"trophyGroupIconUrl"`
		TrophyGroupSmallIconURL string `json:"trophyGroupSmallIconUrl"`
		DefinedTrophies         struct {
			Bronze   int `json:"bronze"`
			Silver   int `json:"silver"`
			Gold     int `json:"gold"`
			Platinum int `json:"platinum"`
		} `json:"definedTrophies"`
		ComparedUser struct {
			OnlineID       string `json:"onlineId"`
			Progress       int    `json:"progress"`
			EarnedTrophies struct {
				Bronze   int `json:"bronze"`
				Silver   int `json:"silver"`
				Gold     int `json:"gold"`
				Platinum int `json:"platinum"`
			} `json:"earnedTrophies"`
			LastUpdateDate time.Time `json:"lastUpdateDate"`
		} `json:"comparedUser"`
	} `json:"trophyGroups"`
}

type TrophySummary

type TrophySummary struct {
	Level          int            `json:"level"`
	Progress       int            `json:"progress"`
	EarnedTrophies EarnedTrophies `json:"earnedTrophies"`
}

type TrophyTitleResponse

type TrophyTitleResponse struct {
	TotalResults int `json:"totalResults"`
	Offset       int `json:"offset"`
	Limit        int `json:"limit"`
	TrophyTitles []struct {
		NpCommunicationID       string `json:"npCommunicationId"`
		TrophyTitleName         string `json:"trophyTitleName"`
		TrophyTitleDetail       string `json:"trophyTitleDetail"`
		TrophyTitleIconURL      string `json:"trophyTitleIconUrl"`
		TrophyTitleSmallIconURL string `json:"trophyTitleSmallIconUrl"`
		TrophyTitlePlatfrom     string `json:"trophyTitlePlatfrom"` // typo in Sony's response
		HasTrophyGroups         bool   `json:"hasTrophyGroups"`
		DefinedTrophies         struct {
			Bronze   int `json:"bronze"`
			Silver   int `json:"silver"`
			Gold     int `json:"gold"`
			Platinum int `json:"platinum"`
		} `json:"definedTrophies"`
		ComparedUser struct {
			OnlineID       string `json:"onlineId"`
			Progress       int    `json:"progress"`
			EarnedTrophies struct {
				Bronze   int `json:"bronze"`
				Silver   int `json:"silver"`
				Gold     int `json:"gold"`
				Platinum int `json:"platinum"`
			} `json:"earnedTrophies"`
			LastUpdateDate time.Time `json:"lastUpdateDate"`
		} `json:"comparedUser"`
		FromUser struct {
			OnlineID       string `json:"onlineId"`
			Progress       int    `json:"progress"`
			EarnedTrophies struct {
				Bronze   int `json:"bronze"`
				Silver   int `json:"silver"`
				Gold     int `json:"gold"`
				Platinum int `json:"platinum"`
			} `json:"earnedTrophies"`
			HiddenFlag     bool      `json:"hiddenFlag"`
			LastUpdateDate time.Time `json:"lastUpdateDate"`
		} `json:"fromUser,omitempty"`
	} `json:"trophyTitles"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL