Request towards Microsoft API returns error 400, with valid accessToken
Unanswered
Cinnamon posted this in #help-forum
CinnamonOP
I'm trying to get a XUID from the
Here's what my provider looks like:
Here's my callback:
And finally, here's my getMinecraft() function, that should normally work but here, doesn't:
I've followed the guide from: https://mojang-api-docs.gapple.pw/authentication/msa
Any help would be appreciated! If you don't understand something feel free to ask.
https://user.auth.xboxlive.com/user/authenticate endpoint. Sadly, every attempt I make, results in an error 400. Here's what my provider looks like:
AzureADProvider({
clientId: process.env.AZURE_AD_CLIENT_ID as string,
clientSecret: process.env.AZURE_AD_CLIENT_SECRET as string,
tenantId: process.env.AZURE_AD_TENANT_ID,
httpOptions: { timeout: 10000 },
authorization: { params: { scope: "openid offline_access profile email"}}
})Here's my callback:
callbacks: {
async jwt({ token, user, account }: { token: any, user: any, account: any }) {
if (account && user) {
const accessToken = await account.access_token
const minecraft = await getMinecraft(accessToken)
return {
accessToken: account.id_token,
accessTokenExpires: account?.expires_at
? account.expires_at * 1000
: 0,
refreshToken: account.refresh_token,
user,
minecraft: minecraft
};
}
if (Date.now() < token.accessTokenExpires - 100000 || 0) {
return token;
}
},
async session({ session, token }: { session: any, token: any }) {
if (session) {
session.user = token.user;
session.error = token.error;
session.accessToken = token.accessToken;
}
return session;
},
},And finally, here's my getMinecraft() function, that should normally work but here, doesn't:
async function getMinecraft(accessToken: string) {
const body1 = JSON.stringify({
"RelyingParty": "http://auth.xboxlive.com",
"TokenType": "JWT",
"Properties": {
"AuthMethod": "RPS",
"SiteName": "user.auth.xboxlive.com",
"RpsTicket": "d="+accessToken
},
})
const res1 = await fetch("https://user.auth.xboxlive.com/user/authenticate", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
"x-xbl-contract-version": "1"
},
body: body1
})
console.log(res1)
console.log(body1)
}I've followed the guide from: https://mojang-api-docs.gapple.pw/authentication/msa
Any help would be appreciated! If you don't understand something feel free to ask.
23 Replies
CinnamonOP
I get the
accessToken, then use it in the request body.CinnamonOP
(The functions aren’t completely, finished, for instance the getMinecraft function doesn’t return anything, I just want to make it work for now then I’ll finish them)
400 indicates the post parameter is not correct. didn’t you get error message from the api? call it with cURL to see details
@tafutada777 400 indicates the post parameter is not correct. didn’t you get error message from the api? call it with cURL to see details
CinnamonOP
Tried with postman, curl, everything, I’m starting to think that my access token is just not valid.
The api returns no error
The api returns no error
did you try without b prefix?
@tafutada777 did you try without b prefix?
CinnamonOP
Yes
you might want to ask their community.
it’s not related to Next.js as it fails with cURL
@tafutada777 you might want to ask their community.
CinnamonOP
Do you know any community? I asked here because it has a link we logging and it’s js so I thought that maybe someone would’ve known
even so, you should have said it fails with cURL in the first place so it saves our time.
If I’m remembering right, Microsoft returns 400s for basically any error on auth. It might be easier to use something like next-auth
Microsoft docs are pretty terrible tbh
in that case it could be 403 so check scope parameter.
@Marchy If I’m remembering right, Microsoft returns 400s for basically any error on auth. It might be easier to use something like next-auth
CinnamonOP
I already use that. What i want to Implement is Minecraft account linking
I used the prisma adapter and right now I'm trying to make the Minecraft linking work
are you sure scope parameters are enough to authorize the api?
the python code in the documentation returns
{
'access_token': 'access token here',
'token_type': 'bearer',
'expires_in': '86400',
'scope': 'service::user.auth.xboxlive.com::MBI_SSL',
'refresh_token': 'refresh token here',
'user_id': 'user id here'
}
'access_token': 'access token here',
'token_type': 'bearer',
'expires_in': '86400',
'scope': 'service::user.auth.xboxlive.com::MBI_SSL',
'refresh_token': 'refresh token here',
'user_id': 'user id here'
}
then a authorization dialog from a web browser, did you see right scopes?
if i were you, i wrote a python code like the official documentation, in order to get an access token, then pass it to cURL.
plus strategy is database by default if prisma used but your code is for jwt strategy
@tafutada777 plus strategy is database by default if prisma used but your code is for jwt strategy
CinnamonOP
I will modify the adapter later, also yes, the authorization pop up is there. I’ll try making a simple code to test the api
yup auth is really tricky i see a lot of questions about it