android客户端登录,从android客户端登录appengine

news/2024/7/8 11:04:28

我正在尝试登录到应用引擎的appengine并访问应用引擎中的用户服务API.基本上我希望能够看到谁登录到我的servlet.我正在使用从

android获取authtoken的身份验证流程,然后从app引擎获取ASID(或SACID)cookie.然后将cookie与http请求一起发送到appengine servlet.这似乎工作得很好,但是当我尝试使用此代码获取用户时:

UserService userService = UserServiceFactory.getUserService();

User user= userService.getCurrentUser();

user始终为null.我的问题是我在这里遗漏了什么?为什么用户服务返回空用户?下面是我的appengine和android代码.任何帮助将不胜感激!

应用引擎:

public class MyServlet extends HttpServlet {

public void process(HttpServletRequest req, HttpServletResponse resp)

throws IOException, ServletException {

resp.setContentType("text/plain");

UserService userService = UserServiceFactory.getUserService();

User user= userService.getCurrentUser();

}

public void doPost(HttpServletRequest req, HttpServletResponse resp)

throws IOException, ServletException {

process(req, resp);

}

public void doGet(HttpServletRequest req, HttpServletResponse resp)

throws IOException, ServletException {

process(req, resp);

}

}

Android代码:

public class AppEngineClient {

static final String BASE_URL = Util.getBaseUrl(this);

private static final String AUTH_URL = BASE_URL + "/_ah/login";

private static final String AUTH_TOKEN_TYPE = "ah";

private final Context mContext;

private final String mAccountName;

private static final String TAG = "AppEngineClient";

public AppEngineClient(Context context, String accountName) {

this.mContext = context;

this.mAccountName = accountName;

}

public HttpResponse makeRequest(String urlPath, List params) throws Exception {

HttpResponse res = makeRequestNoRetry(urlPath, params, false);

if (res.getStatusLine().getStatusCode() == 500) {

res = makeRequestNoRetry(urlPath, params, true);

}

return res;

}

private HttpResponse makeRequestNoRetry(String urlPath, List params, boolean newToken)

throws Exception {

// Get auth token for account

Account account = new Account(mAccountName, "com.google");

String authToken = getAuthToken(mContext, account);

if (newToken) { // invalidate the cached token

AccountManager accountManager = AccountManager.get(mContext);

accountManager.invalidateAuthToken(account.type, authToken);

authToken = getAuthToken(mContext, account);

}

// Get SACSID cookie

DefaultHttpClient client = new DefaultHttpClient();

String continueURL = BASE_URL;

URI uri = new URI(AUTH_URL + "?continue=" +

URLEncoder.encode(continueURL, "UTF-8") +

"&auth=" + authToken);

HttpGet method = new HttpGet(uri);

final HttpParams getParams = new BasicHttpParams();

HttpClientParams.setRedirecting(getParams, false); // continue is not used

method.setParams(getParams);

HttpResponse res = client.execute(method);

Header[] headers = res.getHeaders("Set-Cookie");

if (res.getStatusLine().getStatusCode() != 302 ||

headers.length == 0) {

return res;

}

String sascidCookie = null;

for (Header header: headers) {

if (header.getValue().indexOf("SACSID=") >=0) {

// let's parse it

String value = header.getValue();

String[] pairs = value.split(";");

ascidCookie = pairs[0];

}

}

// Make POST request

uri = new URI(BASE_URL + urlPath);

HttpPost post = new HttpPost(uri);

UrlEncodedFormEntity entity =

new UrlEncodedFormEntity(params, "UTF-8");

post.setEntity(entity);

post.setHeader("Cookie", ascidCookie);

post.setHeader("X-Same-Domain", "1"); // XSRF

res = client.execute(post);

return res;

}

private String getAuthToken(Context context, Account account) throws PendingAuthException {

String authToken = null;

AccountManager accountManager = AccountManager.get(context);

try {

AccountManagerFuture future =

accountManager.getAuthToken (account, AUTH_TOKEN_TYPE, false, null, null);

Bundle bundle = future.getResult();

authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);

if (authToken == null) {

throw new PendingAuthException(bundle);

}

} catch (OperationCanceledException e) {

Log.w(TAG, e.getMessage());

} catch (AuthenticatorException e) {

Log.w(TAG, e.getMessage());

} catch (IOException e) {

Log.w(TAG, e.getMessage());

}

return authToken;

}

public class PendingAuthException extends Exception {

private static final long serialVersionUID = 1L;

private final Bundle mAccountManagerBundle;

public PendingAuthException(Bundle accountManagerBundle) {

super();

mAccountManagerBundle = accountManagerBundle;

}

public Bundle getAccountManagerBundle() {

return mAccountManagerBundle;

}

}

}


http://www.niftyadmin.cn/n/4607824.html

相关文章

ios网络编程(2)

IOS之同步请求、异步请求、GET请求、POST请求 1、同步请求可以从因特网请求数据,一旦发送同步请求,程序将停止用户交互,直至服务器返回数据完成,才可以进行下一步操作, 2、异步请求不会阻塞主线程,而会建立…

mysql ignore lines_自己归纳 MySQL 的一些注意点,劳烦有经验的帮菜鸟确认下对不对,见补充,如果错误,请帮忙指正...

展开全部存在特殊字符e5a48de588b662616964757a686964616f31333332643832情况的处理Book1.csv编号,名称,说明1,测试数据1,"测试CSV文件中,有逗号"2,测试数据2,"测试CSV文件中有""双引号"""3,测试数据3,"测试CSV文件中,有逗号和&q…

浏览器解析html过程,浏览器解析HTML,CSS过程

原标题:浏览器解析HTML,CSS过程每个浏览器都会有自己的呈现引擎,不同内核浏览器之间的解析顺序和方法存在差异,但都是大同小异;a)呈现引擎,呈现引擎一开始会从网络层获取请求文档的内容,内容的大…

Sys.ScriptLoader与JS加载进度条的实现

今天有人问我&#xff0c;163邮箱那样的Javascript加载进度条是如何实现的。 我不知道&#xff0c;不过实现一个不难&#xff0c;因为<script />有onload和onreadystatechange。还有就是&#xff0c;我们有Atlas。 Atlas中有个类&#xff1a;Sys.ScriptLoader&#xff0c…

KNN算法的机器学习基础

本文为 AI 研习社编译的技术博客&#xff0c;原标题 &#xff1a; Machine Learning Basics with the K-Nearest Neighbors Algorithm 翻译 | 小哥哥、江舟 校对 | 吕鑫灿 整理 | 志豪 原文链接&#xff1a; https://towardsdatascience.com/machine-learning-basics…

mysql 所有字符集_mysql字符集

二、查看字符集1.查看MYSQL数据库服务器和数据库字符集mysql> show variables like %character%;------------------------------------------------------| Variable_name | Value |------------------------------------------------------| character_set_client | latin…

ios网络编程(3)

ASIHTTPRequest讲解 ASIHTTPRequest对CFNetwork API进行了封装&#xff0c;并且使用起来非常简单&#xff0c;用Objective-C编写&#xff0c;可以很好的应用在Mac OS X系统和iOS平台的应用程序中。ASIHTTPRequest适用于基本的HTTP请求&#xff0c;和基于REST的服务之间的交互。…

CCNP实战之802.1Q及VLAN的实现

课题内容&#xff1a;研究通过802.1Q实现VLAN知识点&#xff1a;1、PVID在VLAN技术中的作用2、802.1Q标记的实现3、交换机接口在VLAN实现中的工作方法术语&#xff1a;PVID &#xff08;port vlan id&#xff09;实验拓扑&#xff1a;知识点概述&#xff1a;PVID在VLAN技术中…