在不使用客户经理的情况下在Google+中单一登录

时间:2013-09-19 10:06:30

标签: android authentication webview gmail google-oauth

我正在尝试在我的某个应用程序中使用Google +登录。我使用WebView登录用户,在获取auth令牌后,我希望获得一些用户信息,如姓名,电子邮件地址等。

我知道 AccountManager 的情况。

但是我没有使用已存储的帐户,而是希望用户使用WebView登录,并且在成功登录后,我希望将他的电子邮件ID作为信息。

我正在尝试遵循简短的代码。但我得到 401 例外。 java.io.IOException: Unauthorized

WebView webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://mail.google.com");
webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
        Log.d("Home", "Url :" + url);
        String requestToken = "";
        if (url.contains("auth=")) {
            requestToken += url.substring(url.indexOf("auth=") + 5);
            Log.d("Home", "Request Token : " + requestToken);
            requestUrl = "https://www.googleapis.com/oauth2/v1/userinfo?access_token="
                    + requestToken;
            view.stopLoading();
            new CustomThread().start();
        }

    }

    public void onPageFinished(WebView view, String url) {
        // Log.d("Home", "Url :" + url);
        // Log.d("Home", view.getUrl());
    }
});

public class CustomThread extends Thread {
    @Override
    public void run() {
        DefaultHttpClient httpClient = new DefaultHttpClient();

        HttpResponse response;
        try {
            response = httpClient.execute(new HttpGet(requestUrl));
            StatusLine statusLine = response.getStatusLine();
            if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                Log.d("Home", "Response : " + out.toString());

                CookieSyncManager.getInstance().sync();
                // Get the cookie from cookie jar.
                String cookie = CookieManager.getInstance().getCookie(
                        requestUrl);
                if (cookie == null) {
                    Log.d("Home", "Cookie is Null");
                    return;
                }
                // Cookie is a string like NAME=VALUE [; NAME=VALUE]
                String[] pairs = cookie.split(";");
                for (int i = 0; i < pairs.length; ++i) {
                    String[] parts = pairs[i].split("=", 2);
                    // If token is found, return it to the calling activity.
                    if (parts.length == 2
                            && parts[0].equalsIgnoreCase("oauth_token")) {
                        Log.d("Home", "Token :" + parts[1]);
                    }
                }
            } else {
                Log.d("Home", "Error : " + statusLine.getStatusCode());
                // Closes the connection.
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

不使用WebView,对于Google+登录而言,使用PlusClient要容易得多,那么一切都可以使用本机体验。见https://developers.google.com/+/mobile/android/sign-in

答案 1 :(得分:0)

我使用 Social-Auth for Android 使用以下代码执行此操作。

SocialAuthAdapter adapter = new SocialAuthAdapter(new ResponseListener());

imgGoogle.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        adapter.authorize(YourActivity.this, Provider.GOOGLE);
    }
});

private final class ResponseListener implements DialogListener {
    @Override
    public void onComplete(Bundle values) {
        //successful authentication..
    }

    @Override
    public void onError(SocialAuthError error) {
        error.printStackTrace();
    }

    @Override
    public void onCancel() {

    }

    @Override
    public void onBack() {

    }
}