首页 > 解决方案 > Twitter 更新/状态 API 正在使用邮递员而不是在代码 Android 中工作

问题描述

我在 Android 应用程序中集成 Twitter statuses/update.json API。我正在对 API 进行改造。此 API 是必需OAuth 1.0的,我正在生成所有必需的值并调用 API,它给了我以下响应:

{"errors":[{"code":89,"message":"Invalid or expired token."}]}

当我在邮递员中传递所有必需的值时,它正在工作。

这是我将值传递给 Postman 的方式(form-urlencoded):

在此处输入图像描述

以下是我在代码中生成并传入标头的字符串Authorization

OAuth oauth_consumer_key="55pQiiN7hMwSEKMDqc0wYcH",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1617275741",oauth_nonce="675889204f9748a98a7f24c8c313d9bf",oauth_version="1.0",oauth_signature="vuH%2BpCkPNNEr9CnNzT6wXCj2k%3D",oauth_token="2997731-kzHvqPH2NeYnHOV6tFB1wtvXlk1qzUFk3pqUHA",oauth_token_secret="lDOo0Ozyjq2ngdGEr52IOPGxxbsXbWACxZEXLrvlcsO5p",oauth_consumer_secret="tqMmRHJ4kGbykxbOaYD1ZHJgfsKfFSCySpUOGpnIWWH6KjJ"

如上所述,它给了我一个错误。

我如何生成签名:

private static String computeSignature(String baseString, String keyString) throws GeneralSecurityException, UnsupportedEncodingException {
        SecretKey secretKey = null;

        byte[] keyBytes = keyString.getBytes();
        secretKey = new SecretKeySpec(keyBytes, "HmacSHA1");

        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(secretKey);

        byte[] text = baseString.getBytes();

        return new String(Base64.encodeBase64(mac.doFinal(text))).trim();
    } 

我错过了什么吗?如何解决此错误?

标签: androidtwitteroauthpostmantwitter-oauth

解决方案


推荐阅读