#index out of range [0] with length 0

16 messages · Page 1 of 1 (latest)

clear solar
#

Code under:

#
package main

import (
    "bytes"
    "encoding/json"
    "github.com/gin-gonic/gin"
    "io"
    "log"
    "net/http"
    "time"
)

type userHashBody struct {
    Properties struct {
        AuthMethod string `json:"AuthMethod"`
        SiteName   string `json:"SiteName"`
        RpsTicket  string `json:"RpsTicket"`
    } `json:"Properties"`
    RelyingParty string `json:"RelyingParty"`
    TokenType    string `json:"TokenType"`
}
type userHashResponse struct {
    IssueInstant  time.Time `json:"IssueInstant"`
    NotAfter      time.Time `json:"NotAfter"`
    Token         string    `json:"Token"`
    DisplayClaims struct {
        Xui []struct {
            Uhs string `json:"uhs"`
        } `json:"xui"`
    } `json:"DisplayClaims"`
}

type XSTSTokenBody struct {
    Properties struct {
        SandboxId  string   `json:"SandboxId"`
        UserTokens []string `json:"UserTokens"`
    } `json:"Properties"`
    RelyingParty string `json:"RelyingParty"`
    TokenType    string `json:"TokenType"`
}
type XSTSTokenResponse struct {
    IssueInstant  time.Time `json:"IssueInstant"`
    NotAfter      time.Time `json:"NotAfter"`
    Token         string    `json:"Token"`
    DisplayClaims struct {
        Xui []struct {
            Uhs string `json:"uhs"`
        } `json:"xui"`
    } `json:"DisplayClaims"`
}

type BearerTokenBody struct {
    IdentityToken       string `json:"identityToken"`
    EnsureLegacyEnabled bool   `json:"ensureLegacyEnabled"`
}

type BearerTokenResponse struct {
    Username    string        `json:"username"`
    Roles       []interface{} `json:"roles"`
    AccessToken string        `json:"access_token"`
    TokenType   string        `json:"token_type"`
    ExpiresIn   int           `json:"expires_in"`
}```
#

func getUserHash(authToken string) (string, string) {
    var userHashResponse userHashResponse
    var userHashBody = userHashBody{
        Properties: struct {
            AuthMethod string `json:"AuthMethod"`
            SiteName   string `json:"SiteName"`
            RpsTicket  string `json:"RpsTicket"`
        }{
            AuthMethod: "RPS",
            SiteName:   "user.auth.xboxlive.com",
            RpsTicket:  authToken,
        },
        RelyingParty: "http://auth.xboxlive.com",
        TokenType:    "JWT",
    }
    body, _ := json.Marshal(userHashBody)
    if resp, err := http.Post("https://user.auth.xboxlive.com/user/authenticate", "application/json", bytes.NewBuffer(body)); err != nil {
        log.Fatal(err)
    } else {
        defer resp.Body.Close()
        log.Println("Status Code: " + resp.Status + "\n")
        resp.Header.Set("Accept", "application/json")
        body, _ := io.ReadAll(resp.Body)
        json.Unmarshal(body, &userHashResponse)
        UserHash := userHashResponse.DisplayClaims.Xui[0].Uhs
        UserToken := userHashResponse.Token
        return UserHash, UserToken
    }
    return "", ""
}```
#
func getXSTSToken(userToken string) string {
    var XSTSResponse XSTSTokenResponse
    var XSTSBody = XSTSTokenBody{
        Properties: struct {
            SandboxId  string   `json:"SandboxId"`
            UserTokens []string `json:"UserTokens"`
        }{
            SandboxId:  "RETAIL",
            UserTokens: []string{userToken},
        },
        RelyingParty: "rp://api.minecraftservices.com/",
        TokenType:    "JWT",
    }
    body, _ := json.Marshal(XSTSBody)
    if resp, err := http.Post("https://xsts.auth.xboxlive.com/xsts/authorize", "application/json", bytes.NewBuffer(body)); err != nil {
        log.Fatal(err)
    } else {
        log.Println("Status Code: " + resp.Status + "\n")
        defer resp.Body.Close()
        resp.Header.Set("Accept", "application/json")
        body, _ := io.ReadAll(resp.Body)
        json.Unmarshal(body, &XSTSResponse)
        return XSTSResponse.Token
    }
    return ""
}
#
func getBearerToken(authToken string, userHash string) (string, string) {
    var BearerTokenResponse BearerTokenResponse
    var BearerTokenBody = BearerTokenBody{
        IdentityToken:       "XBL3.0 x=" + userHash + ";" + authToken,
        EnsureLegacyEnabled: true,
    }
    body, _ := json.Marshal(BearerTokenBody)
    if resp, err := http.Post("https://api.minecraftservices.com/authentication/login_with_xbox", "application/json", bytes.NewBuffer(body)); err != nil {
        log.Fatal(err)
    } else {
        log.Println("Status Code: " + resp.Status + "\n")
        defer resp.Body.Close()
        resp.Header.Set("Accept", "application/json")
        body, _ := io.ReadAll(resp.Body)
        json.Unmarshal(body, &BearerTokenResponse)
        return BearerTokenResponse.AccessToken, BearerTokenResponse.Username
    }
    return "", ""
}
#

I know what the issue means but i don't know how to fix...

#

Please If someone can fix this

novel slate
#

please don't create duplicate posts, thanks

clear solar
#

I deleted the other one?

novel slate
#

why?

clear solar
#

It was not a good post

#

didn't have the whole code

novel slate
#

you can edit and or post clarifying messages

signal patrol
#

judging by the post title: You can't access the first ([0]) of an empty slice

#

post missing essential information: where is the error

#

You're also ignoring a bunch of errors