我正在開發一個應用程序,它需要訪問帳戶中的所有用戶。 所有操作均使用管理帳戶在Google Apps for Bussiness帳戶中完成。Google App Engine getCurrentUser()返回null
總體思路是從儀表板訪問我的應用程序,不需要任何登錄屏幕/ URL並檢索必要的數據(在這種情況下爲用戶)。那可能嗎?
我不知道是否必須設置OAuth(我的應用程序部署在appspot並添加到GApps帳戶) - 就像我之前說過的,我只是想啓動我的應用程序,它應該從當前獲取登錄憑據記錄GApps用戶。
但是獲取當前用戶返回null。這是我的servlet與方法獲取當前用戶:
public class ExperimentalServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
resp.setContentType("text/plain");
resp.getWriter().println("User: " + (user == null ? "<null>" : user.getNickname()));
}
}
的AppEngine-web.xml中
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>name-of-my-application</application>
<threadsafe>true</threadsafe>
<version>1</version>
</appengine-web-app>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>experimental</servlet-name>
<servlet-class>my.app.domain.ExperimentalServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>experimental</servlet-name>
<url-pattern>/experimental</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
貌似這個(除了重定向怪異名稱):'resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));解決了這個問題。謝謝! –