Servlets oauth获取google shows的请求令牌;您请求的页面无效&引用;
我正在尝试向oauth google发送请求以获取请求令牌。我传递了所有必需的参数。这是代码。请帮助。 下面代码的输出是谷歌提供的一个令牌,用于访问用户的私人数据。但我得到的结果是,我请求的页面无效。我哪里出错了? 我应该在url中也传递范围吗Servlets oauth获取google shows的请求令牌;您请求的页面无效&引用;,servlets,oauth,Servlets,Oauth,我正在尝试向oauth google发送请求以获取请求令牌。我传递了所有必需的参数。这是代码。请帮助。 下面代码的输出是谷歌提供的一个令牌,用于访问用户的私人数据。但我得到的结果是,我请求的页面无效。我哪里出错了? 我应该在url中也传递范围吗 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { try { String o
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();
}
}
}如中所述,您缺少一些必需的参数:
oauth_consumer_key (required)
oauth_nonce (required)
oauth_signature_method (required)
oauth_signature (required)
oauth_timestamp (required)
scope (required)
oauth_callback (required)
您的查询URL也必须采用以下格式:
https://www.google.com/accounts/OAuthGetRequestToken?param1=value1¶m2=value2&...
请注意,已弃用,不应用于新项目:
重要提示:自2012年4月20日起,OAuth 1.0已被正式弃用。它将继续按照我们的弃用政策工作,但我们鼓励您尽快迁移到OAuth 2.0
此外,OAuth2流更简单,请查看