GAE detect running place at localhost or appspot

2019-07-09 20:31发布

I have GAE Application. It uses Oauth 2.0 protocol. I use Java library at the moment. At the moment, I manually switch the redirect URL.

When I'm testing on localhost I use localhost:8888/oauth2callback And when I deploy app, I set example.appspot.com/oauth2callback.

So how can I detect, if the app is running on the localhost or is deployed ?

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-07-09 20:44

As per the documentation, you can do the following

You can check the value of the System property com.google.appengine.runtime.environment to determine if it is "Production" or "Development".

Sample code from docs:

if (SystemProperty.environment.value() ==
SystemProperty.Environment.Value.Production) {
// The app is running on App Engine...
}

and so on.

On the Python side of things, check out the following documentation snippet:

  • SERVER_SOFTWARE: In the development web server, this value is "Development/X.Y" where "X.Y" is the version of the runtime. When running on App Engine, this value is "Google App Engine/X.Y.Z".
查看更多
劫难
3楼-- · 2019-07-09 20:54

You can use oAuthService methods to detect localhost environment

OAuth for Java Overview

[...] On the local development server, oauth.getCurrentUser() always returns a User object with email set to "example@example.com" and user ID set to 0 regardless of whether or not a valid OAuth request was made. [...]

查看更多
登录 后发表回答