前提
Go言語でTwitter APIを使い、ツイートする機能を実装中です。
echoでサーバー起動時後に、localhost:3022/auth
にアクセスするとTwitterのログイン画面を表示するようにしたいのですが、表題のエラーが発生し解決できず困っています。誰か助けてください!
環境
・OS: mac
・言語: Go 1.19
・FW: echo
実現したいこと
- echoでサーバー起動時後に、
localhost:3022/auth
にアクセスするとTwitterのログイン画面を表示するようにしたい。
発生している問題・エラーメッセージ
cli
1authentication failed: OAuth server status 403, <?xml version='1.0' encoding='UTF-8'?><errors><error code="415">Callback URL not approved for this client application. Approved callback URLs can be adjusted in your application settings</error></errors>
該当のソースコード
go run main.go
でRunAPIServer関数を実行しています。
go
1func RunAPIServer() { 2 e := echo.New() 3 e.GET("/auth",AuthTwitter) 4 e.GET("/callback",Callback) 5 6 // サーバー開始 7 go func() { 8 if err := e.Start(":3022"); err != nil { 9 e.Logger.Info("shutting down the server") 10 } 11 }() 12 13 quit := make(chan os.Signal) 14 signal.Notify(quit, os.Interrupt) 15 <-quit 16 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) 17 18 defer cancel() 19 20 if err := e.Shutdown(ctx); err != nil { 21 e.Logger.Fatal(err) 22 } 23} 24 25const ( 26 callback = "https://cat.newstyleservice.net/callback" 27 test = "http://localhost:3022/callback" 28) 29 30// ツイッター認証 31func AuthTwitter(c echo.Context) error { 32 api := getTwitterAPI() 33 var url = callback 34 35 hostname, err := os.Hostname() 36 37 if err != nil { 38 fmt.Printf("failed to retrieve host name: %v\n", err) 39 } 40 41 if strings.Contains(hostname, "local") { 42 url = test 43 } 44 45 // こちらの関数でコケています。 46 uri, _, err := api.AuthorizationURL(url) 47 48 if err != nil { 49 // 該当のエラーログは、下記で出力されています。 50 fmt.Printf("authentication failed: %v\n", err) 51 return err 52 } 53 54 // 成功したらTwitterのログイン画面へ 55 return c.Redirect(http.StatusFound, uri) 56} 57 58// 読み取り後、コールバックから認証まで 59func Callback(c echo.Context) error { 60 token := c.QueryParam("oauth_token") 61 secret := c.QueryParam("oauth_verifier") 62 api := getTwitterAPI() 63 64 cred, _, err := api.GetCredentials(&oauth.Credentials{ 65 Token: token, 66 }, secret) 67 68 if err != nil { 69 fmt.Println(err) 70 return err 71 } 72 73 api = anaconda.NewTwitterApi(cred.Token, cred.Secret) 74 75 sess := session.Default(c) 76 sess.Set("token", cred.Token) 77 sess.Set("secret", cred.Secret) 78 sess.Save() 79 80 return c.Redirect(http.StatusFound, "./tweet") 81}
試したこと
- [Twitter API] OAuthのCallback URLホワイトリスト化に対応するを参考にdeveloper.twitter.comでコールバックURLを追加
- 設定したコールバックURLは、上記
該当コード
の定数で定義しているhttps://cat.newstyleservice.net/callback
- 設定したコールバックURLは、上記
- 環境変数が読み込まれているか確認
- 上記該当コードには記載していません。
- twitter api のカスタマーキーとカスタマーシークレットキーを読み込んでいます。
anaconda
パッケージを使用

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/10/20 22:27