简体   繁体   English

如何在Android中的Oauth中使用令牌和秘密

[英]how to use token and secret in Oauth in android

I'm using Signpost for OAuth in android. 我在Android中使用Signpost进行OAuth。

I am getting Http status code 500 我正在获取Http状态码500

and also getting 并得到

{"messages":{"error":[{"code":401,"message":"oauth_problem=parameter_absent&oauth_parameters_absent=oauth_signature_method"}]}}

Here are the codes: 以下是代码:

            client = new DefaultHttpClient();
            ArrayList<NameValuePair> requestParaRegister = new ArrayList<NameValuePair>();`enter code here`
            requestParaRegister.add(new BasicNameValuePair("firstname",_firstName));
            requestParaRegister.add(new BasicNameValuePair("lastname",_lastName));  
            requestParaRegister.add(new BasicNameValuePair("username",_username));
            requestParaRegister.add(new BasicNameValuePair("email", _emailID));
            requestParaRegister.add(new BasicNameValuePair("password",_password));

HttpPost postRequest = new HttpPost("http://*************************/customers");

//added setsign method. //添加了setsign方法。

consumer.setMessageSigner(new OAuthMessageSigner() { Consumer.setMessageSigner(new OAuthMessageSigner(){

                @Override
                public String sign(HttpRequest arg0, HttpParameters arg1)
                        throws OAuthMessageSignerException {
                    // TODO Auto-generated method stub
                    return OAuth.percentEncode(CONSUMER_SECRET) + "&"
                    + OAuth.percentEncode(_tokenSecret);
                    // consumer.setMessageSigner(OAuth.percentEncode(CONSUMER_SECRET));;
                }

                @Override
                public String getSignatureMethod() {
                    // TODO Auto-generated method stub
                    return null;
                }

    getOauthConsumer().sign(postRequest);
public OAuthConsumer getOauthConsumer() { 
        OAuthConsumer consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); 
        consumer.setTokenWithSecret(_token, _tokenSecret); 
        return consumer; }

Signpost only supports two sugnature methods which are as "PLAINTEXT & HMAC_SHA1". 路标仅支持两种引用方法,例如“ PLAINTEXT&HMAC_SHA1”。 However in your case you should use "HMAC_SHA1". 但是,根据您的情况,应使用“ HMAC_SHA1”。 Refer to the SignatureMethod enum in signpost. 请参阅路标中的SignatureMethod枚举。

In general you need the following params to sign the request 通常,您需要以下参数来签署请求

OAUTH_CONSUMER_KEY
OAUTH_CONSUMER_SECRET
OAUTH_SIGNATURE_METHOD
OAUTH_TIMESTAMP
OAUTH_NONCE
OAUTH_VERSION
OAUTH_TOKEN
OAUTH_TOKEN_SECRET

You can find out more details about oauth at http://tools.ietf.org/pdf/rfc5849.pdf 您可以在http://tools.ietf.org/pdf/rfc5849.pdf上找到有关oauth的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM