go-mastodon icon indicating copy to clipboard operation
go-mastodon copied to clipboard

bad authorization: 400 Bad Request: invalid_grant

Open betandr opened this issue 2 years ago • 9 comments

I know this has been raised before although I can't seem to work out what the issue is. I've tried registering the app in the web UI preferences and also with [go-mastodon](https://github.com/mattn/go-mastodon) and whatever I try I get bad authorization: 400 Bad Request: invalid_grant in response.

I've tried the following code (obviously with the correct values) and I can't get it to work, any ideas I could try?

Thanks!

package main

import (
	"context"
	"log"

	"github.com/mattn/go-mastodon"
)

func main() {
	app, err := mastodon.RegisterApp(context.Background(), &mastodon.AppConfig{
		Server:     "https://the.server",
		ClientName: "my-client",
		Scopes:     "read write follow",
		Website:    "https://github.com/the/path",
	})
	if err != nil {
		log.Fatal(err)
	}

	cl := mastodon.NewClient(&mastodon.Config{
		Server:       "https://the.server",
		ClientID:     app.ClientID,
		ClientSecret: app.ClientSecret,
	})
	err = cl.Authenticate(context.Background(), "[email protected]", "password")
	if err != nil {
		log.Fatal(err)
	}
}

betandr avatar Nov 15 '22 14:11 betandr

Probably, your mastodon server stop to support grant_type=password.

mattn avatar Nov 15 '22 14:11 mattn

Ah, I've added some debug in go-mastodon and it yields: The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client

betandr avatar Nov 15 '22 14:11 betandr

Do you have a code example of using the mastodon.AuthenticateToken function?

betandr avatar Nov 15 '22 15:11 betandr

Do you have a code example of using the mastodon.AuthenticateToken function?

I'm on my phone so I can't write a good example. But checkout how I do it in tut.

Here's how I register a new account in my program.

https://github.com/RasmusLindroth/tut/blob/2634818c0a1097da935d7137b3620249e679223b/auth/add.go

To use the access token once you have generated it you'll just have to include it in your config like this.

conf := &mastodon.Config{
	Server:       acc.Server,
	ClientID:     acc.ClientID,
	ClientSecret: acc.ClientSecret,
	AccessToken:  acc.AccessToken,
}

RasmusLindroth avatar Nov 16 '22 22:11 RasmusLindroth

That works @RasmusLindroth. However, the AuthenticateToken() works only once. Reuse of the same code seems to be forbidden. Do you have an idea on how to reuse the code? I obviously don't want to register a new app each time I restart my tool

sbani avatar Jan 09 '23 06:01 sbani

That works @RasmusLindroth. However, the AuthenticateToken() works only once. Reuse of the same code seems to be forbidden. Do you have an idea on how to reuse the code? I obviously don't want to register a new app each time I restart my tool

You just run AuthenticateToken() once, then you use:

conf := &mastodon.Config{
	Server:       acc.Server,
	ClientID:     acc.ClientID,
	ClientSecret: acc.ClientSecret,
	AccessToken:  acc.AccessToken,
}
client := mastodon.NewClient(conf)

Now you can use the client for your requests

RasmusLindroth avatar Jan 09 '23 11:01 RasmusLindroth

Wow, it's so easy. That works, thank you!

sbani avatar Jan 09 '23 12:01 sbani

The example on the main page fails.

c := mastodon.NewClient(&mastodon.Config{ Server: "https://mstdn.jp", ClientID: "client-id", ClientSecret: "client-secret", }) err := c.Authenticate(context.Background(), "your-email", "your-password") if err != nil { log.Fatal(err) }

atleast when i tried. Need to add accesskey and remove the authenticate part.

ktpx avatar Aug 27 '23 09:08 ktpx