android – Twitter OAuth Rest Api状态参数’@’符号

android – Twitter OAuth Rest Api状态参数’@’符号,第1张

概述我正在使用twitter rest api,这是( https://api.twitter.com/1.1/). 首先,我使用路标库生成oauth_signature.它运作良好. 上传状态端点(https://api.twitter.com/1.1/statuses/upload.json)运行良好,但如果状态参数包含“@”符号,则表示无效.所以这是我的代码 TwitterStatusesSer 我正在使用twitter rest API,这是( https://api.twitter.com/1.1/).

首先,我使用路标库生成oauth_signature.它运作良好.

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

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).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."}]}

但是,如果我将使用状态变量“只是示例”而不是“@example”(具体点是删除@字符)正在工作.只有’@’符号不起作用.

编辑

这是我通过改装拦截器手动创建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,"ID");                    parameterString = OAuthParams.addParam(request,"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,"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));}}
解决方法 我建议使用面料 – https://fabric.io,它有各种各样的libs,如twitter登录,crashlytics和alss,你需要设置与AndroID工作室的面料插件.发布您可以发布/检索推文,无论您想发布什么.

对于您的问题 – 检查’@’是特殊符号,其中改造导致问题.这些问题是我在不同场景中遇到的改造.

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

总结

以上是内存溢出为你收集整理的android – Twitter OAuth Rest Api状态参数’@’符号全部内容,希望文章能够帮你解决android – Twitter OAuth Rest Api状态参数’@’符号所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1136204.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存