2

我正在尝试在 Android 上实现 OpenId 并不断收到 400 错误。我猜我的参数格式不正确。

我将参数基于此浏览器行,它可以正确地将我带到服务器:

https://www.google.com/accounts/o8/ud
?openid.ns=http://specs.openid.net/auth/2.0
&openid.claimed_id=http://specs.openid.net/auth/2.0/identifier_select
&openid.identity=http://specs.openid.net/auth/2.0/identifier_select
&openid.return_to=http://www.example.com/checkauth
&openid.realm=http://www.example.com/
&openid.assoc_handle=ABSmpf6DNMw
&openid.mode=checkid_setup

这是代码

            try {

         HttpsURLConnection con; 

         URL url2 = new URL("https://www.google.com/accounts/o8/ud");
         con = (HttpsURLConnection) url2.openConnection();
         con.setDoInput(true);
         con.setDoOutput(true);

         con.setRequestMethod("GET");

         List<NameValuePair> params = new ArrayList<NameValuePair>();
         params.add(new BasicNameValuePair("openid.ns", "http://specs.openid.net/auth/2.0"));
         params.add(new BasicNameValuePair("openid.claimed_id", "http://specs.openid.net/auth/2.0/identifier_select"));
         params.add(new BasicNameValuePair("openid.identity", "http://specs.openid.net/auth/2.0/identifier_select"));
         params.add(new BasicNameValuePair("openid.return_to", "http://www.example.com/checkauth"));
         params.add(new BasicNameValuePair("openid.realm", "http://www.example.com/"));
         params.add(new BasicNameValuePair("openid.assoc_handle", "ABSmpf6DNMw"));
         params.add(new BasicNameValuePair("openid.mode", "checkid_setup"));

         String content = AuthUtils.getRequestParams(params);
         con.setFixedLengthStreamingMode(content.getBytes("UTF-8").length);

         OutputStream out = con.getOutputStream();
         BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
         writer.write(content);
         writer.close();
         out.close();            

         Log.d(TAG, "response code = " + con.getResponseCode());

         BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));

         String input;

         while ((input = br.readLine()) != null){
             System.out.println(input);
         }
         br.close();             


        } catch (MalformedURLException e) {
             Log.e(TAG, e.toString());
            e.printStackTrace();
        } catch (IOException e) {
             Log.e(TAG, e.toString());
         e.printStackTrace();
        } catch (Exception e) {
             Log.e(TAG, e.toString());
             e.printStackTrace();
        }
4

1 回答 1

0

来自谷歌的文档

如果提供的域不是由 Google 托管, Google 的端点将在端点 (2) 上返回 400。因此,对于想要迎合 Google Apps 客户的 RP,一种可能的策略是首先尝试端点 (2),如果返回 400,则尝试端点 (1),如果没有产生任何结果,则放弃。其他策略(例如,并行获取两个端点)是可能的。

更多信息在这里

于 2014-01-06T12:30:56.973 回答