mattn / go-mastodon

mastodon client for golang
MIT License
599 stars 85 forks source link

bad authorization: 400 Bad Request: invalid_grant #154

Open betandr opened 1 year ago

betandr commented 1 year ago

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 commented 1 year ago

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

betandr commented 1 year ago

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 commented 1 year ago

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

RasmusLindroth commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

ktpx commented 11 months ago

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.