jjzjj

android - Twitter OAuth Rest Api 状态参数 '@' 符号

coder 2023-11-26 原文

我正在使用 Twitter rest api,它是 ( https://api.twitter.com/1.1/ )。

首先,我使用 signpost 库来生成 oauth_signature。它运行良好。

上传状态端点 ( https://api.twitter.com/1.1/statuses/upload.json ) 运行良好,但如果 status 参数包含“@” 符号,则无法正常工作。所以这是我的代码

TwitterStatusesService.java

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Query;

public interface TwitterStatusesService {
    @POST("/1.1/statuses/update.json")
    Call<ResponseBody> update(@Query("status") String status, @Query("in_reply_to_status_id") String inReplyToStatusId, @Query("lat") Double lat, @Query("long") Double lon, @Query("media_ids") String mediaIds);
}

TwitterStatusesAPIClient.java

import android.util.Log;

import com.twitter.sdk.android.core.TwitterAuthToken;
import com.twitter.sdk.android.core.TwitterCore;
import com.twitter.sdk.android.core.TwitterSession;

import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Retrofit;
import se.akerfeldt.okhttp.signpost.OkHttpOAuthConsumer;
import se.akerfeldt.okhttp.signpost.SigningInterceptor;

public class TwitterStatusesClient {

    private final String TAG = getClass().getSimpleName();

    private static final String BASE_URL = "https://api.twitter.com/";

    private final TwitterStatusesService apiService;

    private static TwitterStatusesClient webServiceClient;

    public static TwitterStatusesClient getInstance() {
        if (webServiceClient == null)
            webServiceClient = new TwitterStatusesClient();
        return webServiceClient;
    }

    private TwitterStatusesClient() {
        private TwitterStatusesClient() {
        OkHttpOAuthConsumer consumer = new OkHttpOAuthConsumer(TWITTER_KEY, TWITTER_SECRET);

        TwitterSession activeSession = TwitterCore.getInstance().getSessionManager().getActiveSession();
        if (activeSession != null) {
            TwitterAuthToken authToken = activeSession.getAuthToken();
            String token = authToken.token;
            String secret = authToken.secret;
            consumer.setTokenWithSecret(token, secret);
        }

        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new SigningInterceptor(consumer))
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(client)
                .build();

        apiService = retrofit.create(TwitterStatusesService.class);
    }

    public Call<ResponseBody> update(String status, String statusId, Double lat, Double lon, String mediaIds) {
        return apiService.update(status, statusId, lat, lon, mediaIds);
    }
}

调用api客户端

String status = "@example";
TwitterStatusesClient.getInstance().update(status, null, null, null, null).enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            Log.v(TAG, "onResponse");
            progressDialog.dismiss();
            try {
                if (response.errorBody() != null) {
                    String error = response.errorBody().string();
                    Log.e(TAG, "Error : " + error);
                    ToastUtils.showErrorMessage(getContext(), "Error : " + error);
                    return;
                }

                String body = response.body().string();
                Log.v(TAG, "body : " + body);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.v(TAG, "onFailure");
            t.printStackTrace();
        }
    });

给出错误:

{"errors":[{"code":32,"message":"Could not authenticate you."}]}

但是,如果我将使用状态变量“just example”而不是“@example”(具体点是删除 @ 字符),那是可行的。只有“@”符号不起作用。

编辑

这是我通过 retrofit 拦截器手动创建 OAuth v1.0a 签名签名代码:

OkHttpClient client = new OkHttpClient.Builder()
            .addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Interceptor.Chain chain) throws IOException {
                    Request request = chain.request();

                    String method = request.method();
                    String baseUrl = "https://api.twitter.com" + request.url().url().getPath();

                    String oauthToken = "";
                    String oauthTokenSecret = "";

                    TwitterSession activeSession = TwitterCore.getInstance().getSessionManager().getActiveSession();
                    if (activeSession != null) {
                        TwitterAuthToken authToken = activeSession.getAuthToken();
                        oauthToken = authToken.token;
                        oauthTokenSecret = authToken.secret;
                    }

                    String oauthNonce = "TXZScw4M8TG";
                    String oauthSignatureMethod = "HMAC-SHA1";
                    String oauthTimestamp = String.valueOf(System.currentTimeMillis() / 1000);
                    String oauthVersion = "1.0";

                    String parameterString = "";

                    parameterString = OAuthParams.addParam(request, parameterString, "count");
                    parameterString = OAuthParams.addParam(request, parameterString, "id");
                    parameterString = OAuthParams.addParam(request, parameterString, "in_reply_to_status_id");

                    // if any parameter added to parameterString, append '&' character.
                    if (parameterString.length() > 0) {
                        parameterString += "&";
                    }

                    parameterString += "oauth_consumer_key=" + TWITTER_KEY + "&"
                            + "oauth_nonce=" + oauthNonce + "&"
                            + "oauth_signature_method=" + oauthSignatureMethod + "&"
                            + "oauth_timestamp=" + oauthTimestamp + "&"
                            + "oauth_token=" + oauthToken + "&"
                            + "oauth_version=" + oauthVersion;

                    // add status parameter to parameterString.
                    parameterString = OAuthParams.addParam(request, parameterString, "status");

                    Log.d(TAG, "normalizedParameters : " + parameterString);
                    Log.d(TAG, "parameterStringPercent : " + OAuth.percentEncode(parameterString));

                    String signatureBaseString = "";
                    signatureBaseString += OAuth.percentEncode(method) + "&";
                    signatureBaseString += OAuth.percentEncode(baseUrl) + "&";
                    signatureBaseString += OAuth.percentEncode(parameterString);

                    String oauthSignature = OauthSignature.generateSignature(signatureBaseString, TWITTER_SECRET,
                            oauthTokenSecret);

                    String authorization = "OAuth oauth_consumer_key=\"" + TWITTER_KEY + "\", " +
                            "oauth_signature_method=\"HMAC-SHA1\", " +
                            "oauth_timestamp=\"" + oauthTimestamp + "\", " +
                            "oauth_nonce=\"" + oauthNonce + "\", " +
                            "oauth_version=\"1.0\", " +
                            "oauth_token=\"" + oauthToken + "\", " +
                            "oauth_signature=\"" + OAuth.percentEncode(oauthSignature) + "\"";

                    Log.w(TAG, "Authorization : " + authorization);

                    request = request.newBuilder()
                            .addHeader("Authorization", authorization)
                            .build();
                    return chain.proceed(request);
                }
            }).addInterceptor(interceptor).build();

OAuth.java

public static String percentEncode(String s) {
    if (s == null) {
        return "";
    }
    try {
        return URLEncoder.encode(s, ENCODING)
                // OAuth encodes some characters differently:
                .replace("+", "%20").replace("*", "%2A")
                .replace("%7E", "~");
        // This could be done faster with more hand-crafted code.
    } catch (UnsupportedEncodingException wow) {
        throw new RuntimeException(wow.getMessage(), wow);
    }
}

OAuthSignature.java

import android.util.Base64;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class OauthSignature {

public static String generateSignature(String signatueBaseStr, String oAuthConsumerSecret, String oAuthTokenSecret) {
    byte[] byteHMAC = null;
    try {
        Mac mac = Mac.getInstance("HmacSHA1");
        SecretKeySpec spec;
        if (null == oAuthTokenSecret) {
            String signingKey = OAuth.percentEncode(oAuthConsumerSecret) + '&';
            spec = new SecretKeySpec(signingKey.getBytes(), "HmacSHA1");
        } else {
            String signingKey = OAuth.percentEncode(oAuthConsumerSecret) + '&' + OAuth.percentEncode(oAuthTokenSecret);
            spec = new SecretKeySpec(signingKey.getBytes(), "HmacSHA1");
        }
        mac.init(spec);
        byteHMAC = mac.doFinal(signatueBaseStr.getBytes());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new String(Base64.encode(byteHMAC, Base64.DEFAULT));
}
}

最佳答案

我建议选择 Fabric - https://fabric.io它有 twitter 各种库,如 twitter 登录、crashlytics 和 alss,你需要使用 android studio 设置 fabric 插件。发布你将能够发布/检索你的推文,无论你想发布什么。

对于您的问题 - 检查“@”是否为改造导致问题的特殊符号。我在不同的场景中遇到过这样的改造问题。

尝试使用 Fabric 进行 Twitter 登录/oAuth

关于android - Twitter OAuth Rest Api 状态参数 '@' 符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36743311/

有关android - Twitter OAuth Rest Api 状态参数 '@' 符号的更多相关文章

  1. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  2. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  3. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  4. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  5. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  6. ruby - 在 jRuby 中使用 'fork' 生成进程的替代方案? - 2

    在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',

  7. ruby - RSpec - 使用测试替身作为 block 参数 - 2

    我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere

  8. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  9. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  10. ruby - 如何在 Ruby 中拆分参数字符串 Bash 样式? - 2

    我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"

随机推荐