gpt4 book ai didi

java - 在 Java 中实现 OAuth

转载 作者:搜寻专家 更新时间:2023-10-31 20:31:09 24 4
gpt4 key购买 nike

我曾尝试在 Java 中为我的编程想法实现 OAuth,但我惨遭失败。我不知道为什么,但我的代码不起作用。每次我运行程序时,都会抛出一个 IOException,原因是“java.io.IOException:服务器返回 HTTP 响应代码:401”(401 表示未经授权)。我仔细查看了文档,但我真的不明白为什么它不起作用。我想使用的 OAuth 提供商是 twitter,我也在那里注册了我的应用程序。
提前致谢
飞哥


OAuth docs
Twitter API wiki
Class Base64Coder

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.net.URLConnection;
import java.net.MalformedURLException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;

public class Request {
public static String read(String url) {
StringBuffer buffer = new StringBuffer();
try {
/**
* get the time - note: value below zero
* the millisecond value is used for oauth_nonce later on
*/
int millis = (int) System.currentTimeMillis() * -1;
int time = (int) millis / 1000;

/**
* Listing of all parameters necessary to retrieve a token
* (sorted lexicographically as demanded)
*/
String[][] data = {
{"oauth_callback", "SOME_URL"},
{"oauth_consumer_key", "MY_CONSUMER_KEY"},
{"oauth_nonce", String.valueOf(millis)},
{"oauth_signature", ""},
{"oauth_signature_method", "HMAC-SHA1"},
{"oauth_timestamp", String.valueOf(time)},
{"oauth_version", "1.0"}
};

/**
* Generation of the signature base string
*/
String signature_base_string =
"POST&"+URLEncoder.encode(url, "UTF-8")+"&";
for(int i = 0; i < data.length; i++) {
// ignore the empty oauth_signature field
if(i != 3) {
signature_base_string +=
URLEncoder.encode(data[i][0], "UTF-8") + "%3D" +
URLEncoder.encode(data[i][1], "UTF-8") + "%26";
}
}
// cut the last appended %26
signature_base_string = signature_base_string.substring(0,
signature_base_string.length()-3);

/**
* Sign the request
*/
Mac m = Mac.getInstance("HmacSHA1");
m.init(new SecretKeySpec("CONSUMER_SECRET".getBytes(), "HmacSHA1"));
m.update(signature_base_string.getBytes());
byte[] res = m.doFinal();
String sig = String.valueOf(Base64Coder.encode(res));
data[3][1] = sig;

/**
* Create the header for the request
*/
String header = "OAuth ";
for(String[] item : data) {
header += item[0]+"=\""+item[1]+"\", ";
}
// cut off last appended comma
header = header.substring(0, header.length()-2);

System.out.println("Signature Base String: "+signature_base_string);
System.out.println("Authorization Header: "+header);
System.out.println("Signature: "+sig);

String charset = "UTF-8";
URLConnection connection = new URL(url).openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
connection.setRequestProperty("Authorization", header);
connection.setRequestProperty("User-Agent", "XXXX");
OutputStream output = connection.getOutputStream();
output.write(header.getBytes(charset));

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

String read;
while((read = reader.readLine()) != null) {
buffer.append(read);
}
}
catch(Exception e) {
e.printStackTrace();
}

return buffer.toString();
}

public static void main(String[] args) {
System.out.println(Request.read("http://api.twitter.com/oauth/request_token"));
}
}

最佳答案

尝试删除参数“oauth_callback”。它对我有用。我正在处理的应用程序是一个 Web 应用程序。

关于java - 在 Java 中实现 OAuth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2964392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com