gpt4 book ai didi

android - Tumblr Xauth Android - 400 错误请求错误

转载 作者:行者123 更新时间:2023-11-30 04:22:54 27 4
gpt4 key购买 nike

我正在尝试将 Xuath 与 Tumblr 一起使用。我已经通过电子邮件向 Tumblr 支持团队发送电子邮件,要求为我的应用程序启用 Xuath,他们同意了。但是,在尝试检索用户 key 和密码时,我不断收到“400:错误请求”错误。我找不到调试错误请求的方法。

以下是代码:(注意 - 此代码是根据网络上可用的 fragment 开发的)

private final String CONSUMER_KEY = "mdMFLrprZGnRw4XO736GXcXP8huxaxTT5z1nlxDK38GbyWlW38";
private final String CONSUMER_SECRET = "VOpRNqKSLjhD3bR8vw4MorXgGc7lkT2FtBZr9xDchA5AvfscUI";

private final String ACCESS_URL = "https://www.tumblr.com/oauth/access_token";
private final String XAUTH_MODE = "client_auth";
private final String SIGNATURE_METHOD = "HMAC-SHA1";
private final String OAUTH_VERSION = "1.0";


private EditText mEmailAddress;
private EditText mPassword;
private Button mLogInButton;



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.tumblr_layout);

mEmailAddress = (EditText) findViewById(R.id.email_tumblr);
mPassword = (EditText) findViewById(R.id.passowrd_tumblr);
mLogInButton = (Button) findViewById(R.id.tumblr_login_button);

mLogInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = mEmailAddress.getText().toString();
String password= mPassword.getText().toString();



String oauth_nonce = a64BitRandomString();
String oauth_timestamp = getTimeStamp();


String signatureBaseString =
"POST"
+ "&"
+ URLEncoder.encode(ACCESS_URL)
+ "&"
+ URLEncoder.encode("oauth_consumer_key=" + URLEncoder.encode(CONSUMER_KEY))
+ URLEncoder.encode("&" + "oauth_nonce=" + URLEncoder.encode(oauth_nonce))
+ URLEncoder.encode("&" + "oauth_signature_method=" + URLEncoder.encode(SIGNATURE_METHOD))
+ URLEncoder.encode("&" + "oauth_timestamp=" + URLEncoder.encode(oauth_timestamp))
+ URLEncoder.encode("&" + "oauth_version=" + URLEncoder.encode(OAUTH_VERSION))
+ URLEncoder.encode("&" + "x_auth_username=" + URLEncoder.encode(email))
+ URLEncoder.encode("&" + "x_auth_password=" + URLEncoder.encode(password))
+ URLEncoder.encode("&" + "x_auth_mode=" + URLEncoder.encode(XAUTH_MODE));


String oauth_signature= getSignature(signatureBaseString, "HmacSHA1",
CONSUMER_SECRET+"&");

try {
String headerValue = "OAuth " +
"oauth_nonce=\""+oauth_nonce+"\"," +
"oauth_signature_method=\""+SIGNATURE_METHOD+"\"," +
"oauth_timestamp=\""+oauth_timestamp+"\"," +
"oauth_consumer_key=\""+CONSUMER_KEY+"\"," +
"oauth_signature=\""+URLEncoder.encode(oauth_signature,"UTF-8")+"\"," +
"oauth_version=\""+OAUTH_VERSION+"\"";

HttpPost httppost = new HttpPost(ACCESS_URL
+"?x_auth_username="+URLEncoder.encode(email)
+"&x_auth_password="+URLEncoder.encode(password)
+"&x_auth_mode="+URLEncoder.encode(XAUTH_MODE));

httppost.setHeader("Host","https://www.tumblr.com");
httppost.setHeader("Content-Type","application/x-www-form-urlencoded");
httppost.setHeader("Authorization",headerValue);


// Execute HTTP Post Request
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost); // **I get the 401 error here**
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
String jString= EntityUtils.toString(entity);
Log.d("TUMBLR - Value(s):", jString);
}

} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
});

}

private String a64BitRandomString() {
StringBuffer sb = new StringBuffer();
Random generator = new Random();

for (int i = 0; i < 32; i++) {
Integer r = generator.nextInt();
if (r < 0) {
r = r * -1;
}
r = r % 16;

sb.append(Integer.toHexString(r));
}

return sb.toString();
}


private String getTimeStamp(){
long seconds = (long) (System.currentTimeMillis()/1000.0);
String secondsString = String.valueOf(seconds);
return secondsString;
}

private String getSignature(String base, String mode, String secret) {
String signature = null;


SecretKeySpec key;
try {
key = new SecretKeySpec((secret).getBytes("UTF-8"), mode);

Mac mac = Mac.getInstance(mode);
mac.init(key);

byte[] bytes = mac.doFinal(base.getBytes("UTF-8"));

signature = new String(Base64.encode(bytes,Base64.NO_WRAP));
}
catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return signature;
}

我知道要在这里找出确切的错误是非常困难的。但是,如果有人可以提供建议或为我指明正确的方向,我会很高兴。

String headerValue 具有以下值:

OAuth oauth_nonce="8d0e6e03ae2424260ddd647d5afba70d",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1327434351943",oauth_consumer_key="mdMFLrprZGnRw4XO736GXcXP8huxaxTT5z1 nlxDK38GbyWlW38",oauth_signature="cYNStrfA%2F2lTaGKL8pxWHpzSq9w%3D",oauth_version="1.0"

所以,它的格式似乎是正确的。在字符串中包含换行符会有帮助吗?

最佳答案

在我焦头烂额几天后,我发现我在 HTTP post 正文中发送了太多信息(因此,向我返回了一个错误的请求错误)。所需要的只是以下一组参数:

x_auth_username(用户的电子邮件地址)、x_auth_password 和 x_auth_mode=client_auth

此外,我应该利用强大的 Oauth 库函数,而不是尝试使用我有限的知识编写自己的函数。在我看来,无论如何,文档从来都不够清楚。一个很好的教训——关于重用代码和使用常识。对于那些可能想知道是否有用于 TUMBLR 的合法 Java 客户端的人 - 这里是 github 链接:https://github.com/nsheridan/tumblr-java .我向你保证,这是我遇到过的最好的 Tumblr 代码。

关于android - Tumblr Xauth Android - 400 错误请求错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8994888/

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