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

我正在尝试登录到应用引擎的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;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值