Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bad authorization: 400 Bad Request: invalid_grant #154

Open
betandr opened this issue Nov 15, 2022 · 8 comments
Open

bad authorization: 400 Bad Request: invalid_grant #154

betandr opened this issue Nov 15, 2022 · 8 comments

Comments

@betandr
Copy link

betandr commented Nov 15, 2022

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(), "user@example.com", "password")
	if err != nil {
		log.Fatal(err)
	}
}
@mattn
Copy link
Owner

mattn commented Nov 15, 2022

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

@betandr
Copy link
Author

betandr commented Nov 15, 2022

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
Copy link
Author

betandr commented Nov 15, 2022

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

@RasmusLindroth
Copy link
Contributor

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,
}

@sbani
Copy link

sbani commented Jan 9, 2023

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

@RasmusLindroth
Copy link
Contributor

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

@sbani
Copy link

sbani commented Jan 9, 2023

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

@ktpx
Copy link

ktpx commented Aug 27, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants