【发布时间】:2012-07-13 16:44:19
【问题描述】:
我正在尝试向 oauth google 发送请求以获取请求令牌。我传递了所需的所有参数。这是代码。请帮助。 以下代码的输出是谷歌提供的用于访问其用户私人数据的令牌。但我得到的结果是我请求的页面无效。我哪里出错了? 我也应该在 url 中传递范围吗?
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();
}
}
}
【问题讨论】: