无法获取登录用户信息

huangapple go评论166阅读模式
英文:

go Unable to get login user information

问题

<p>你好。我正在使用Google App Engine中的Go语言。我在获取已登录用户的信息方面遇到了问题。同样地,无法获取登录URL和注销URL。所有的值都将返回nil。user.IsAdmin(c)返回false。请帮助我。</p>

<pre><code>

admin.go

func Entry(w http.ResponseWriter, r *http.Request) {
...
c := appengine.NewContext(r)

inUrl, err := user.LoginURL(c, &quot;/admin/top/&quot;)
...

}

func AdminTop(w http.ResponseWriter, r *http.Request) {
...
c := appengine.NewContext(r)
booisadmin := user.IsAdmin(c)
u := user.Current(c)
outUrl, err := user.LogoutURL(c, "/")
...
}

</code></pre>

<pre><code>

app.yaml

runtime: go116

app_engine_apis: true

handlers:

  • url: /assets/css
    mime_type: text/css
    static_dir: assets/css

  • url: /assets/html
    mime_type: text/html
    static_dir: assets/html

  • url: /assets/img
    static_dir: assets/img

  • url: /admin/.*
    login: require
    script: _go_app

  • url: /.*
    script: _go_app

</code></pre>

英文:

<p>Hello. I'm using the go language in Google App Engine. I'm having trouble getting the logged-in user's information. Similarly, the login URL and logout URL cannot be obtained. All nil will be returned. user.IsAdmin (c) returns false. please help me.</p>

<pre><code>

admin.go

func Entry(w http.ResponseWriter, r *http.Request) {
...
c := appengine.NewContext(r)

inUrl, err := user.LoginURL(c, &quot;/admin/top/&quot;)
...

}

func AdminTop(w http.ResponseWriter, r *http.Request) {
...
c := appengine.NewContext(r)
booisadmin := user.IsAdmin(c)
u := user.Current(c)
outUrl, err := user.LogoutURL(c, "/")
...
}

</code></pre>

<pre><code>

app.yaml

runtime: go116

app_engine_apis: true

handlers:

  • url: /assets/css
    mime_type: text/css
    static_dir: assets/css

  • url: /assets/html
    mime_type: text/html
    static_dir: assets/html

  • url: /assets/img
    static_dir: assets/img

  • url: /admin/.*
    login: require
    script: _go_app

  • url: /.*
    script: _go_app

</code></pre>

答案1

得分: 1

当你在app.yaml中使用login: required时,你可以通过以下头部信息获取已登录用户的信息:

  • X-Appengine-User-Id
  • X-Appengine-User-Nickname
  • X-Appengine-User-Email

我确认在Go中可以使用上述头部信息(在我的本地机器上运行过)。

我相信当你使用用户API时,相同的头部信息也可以使用,但你可以通过转储所有头部信息来确定你需要的值。

关于使用User API获取登录/注销URL,当我在本地机器上尝试时,我也得到了空值,但我在Go方面是个新手。当你部署到生产环境时,你可以尝试一下看这些调用是否有效。

英文:

When you use login: required in app.yaml, you can get the logged in users information via the following headers -

  • X-Appengine-User-Id
  • X-Appengine-User-Nickname
  • X-Appengine-User-Email

I confirmed the above works in Go (ran it on my local machine)

I believe the same headers should work when you use the Users API but you can always dump all the headers to figure out the values that you need.

Regarding using the User API to get login/logout urls, I also got blank values when I tried it on my local machine but I'm a novice when it comes to Go. You might want to try and see if the calls work when you deploy to Production

答案2

得分: 0

谢谢。我已经解决了。问题是因为我使用了Gorillamux。我用以下代码解决了问题。

import (
    ...
    "github.com/gorilla/mux"
    "net/http"
    ...
)

func main() {
    r := mux.NewRouter()
    r.HandleFunc("/", indexHandler)
    r.HandleFunc("/admin/", admin.Entry)
    http.Handle("/", r)
}

最后一个http.Handle("/", r)缺失了。

我在这里写了详细信息。
https://uubaago.blogspot.com/

非常感谢NoCommandLine!

英文:

Thank you. I was able to solve it. It was because I was using Gorillamux. I solved it with the following code.

import (
    ...
    &quot;github.com/gorilla/mux&quot;
    &quot;net/http&quot;
    ...
)

func main() {
    r := mux.NewRouter()
    r.HandleFunc(&quot;/&quot;, indexHandler)
    r.HandleFunc(&quot;/admin/&quot;, admin.Entry)
    http.Handle(&quot;/&quot;, r)
}

The last http.Handle ("/", r) was missing.

I wrote the details here.
https://uubaago.blogspot.com/

Thank you very much NoCommandLine!

huangapple
  • 本文由 发表于 2022年3月1日 13:24:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/71304071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定