oauth获取谷歌节目的请求令牌"您请求的页面无效。 "

时间:2012-07-13 05:30:05

标签: servlets oauth

我正在尝试向oauth google发送请求以获取请求token.i传递了所有必需的参数。这是code.please帮助。 以下代码的输出是谷歌提供的访问其用户私人数据的令牌。但我得到的结果是我请求的页面无效。我哪里出错? 我应该在网址中传递范围吗?

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    try {  
          String oauthConsumerKey = "my key";
          String oauthSignatureMethod = "HMAC-SHA1";
          String oauthSignature = "my signature";
        // Send the request  
        URL url = new URL("https://www.google.com/accounts/OAuthGetRequestToken"+oauthConsumerKey+
        oauthSignatureMethod+oauthSignature);  
        URLConnection conn = url.openConnection();  
        conn.setDoOutput(true);  
        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());  

        //write parameters   

        writer.flush();  

        // Get the response  
        StringBuffer answer = new StringBuffer();  
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));  
        String line;  
        while ((line = reader.readLine()) != null) {  
           System.out.println("\n" +line);

        }  

        writer.close();  
        reader.close();  
        //Output the response  


    } catch (MalformedURLException ex) {  
        ex.printStackTrace();  
    } catch (IOException ex) {  
        ex.printStackTrace();  
    }  
}

}

1 个答案:

答案 0 :(得分:0)

documentation of the OAuthGetRequestToken call中所述,您缺少一些必需的参数:

oauth_consumer_key     (required)
oauth_nonce            (required)
oauth_signature_method (required)
oauth_signature        (required)
oauth_timestamp        (required)
scope                  (required)
oauth_callback         (required)

您的查询网址也必须采用以下格式:

https://www.google.com/accounts/OAuthGetRequestToken?param1=value1&param2=value2&...

请注意,Google OAuth 1.0 API已弃用,不应用于新项目:

  

重要提示:截至2012年4月20日,OAuth 1.0已正式弃用。它将继续按照我们的弃用政策运行,但我们建议您尽快迁移到OAuth 2.0。

此外,OAuth 2流程更加简单,请查看the documentation at Google