英文:
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, "/admin/top/")
...
}
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, "/admin/top/")
...
}
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 (
...
"github.com/gorilla/mux"
"net/http"
...
)
func main() {
r := mux.NewRouter()
r.HandleFunc("/", indexHandler)
r.HandleFunc("/admin/", admin.Entry)
http.Handle("/", r)
}
The last http.Handle ("/", r) was missing.
I wrote the details here.
https://uubaago.blogspot.com/
Thank you very much NoCommandLine!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论