Scribe OAuth不在android上运行,错误500

时间:2014-05-22 10:16:29

标签: android oauth scribe

我使用以下代码访问Magento api。我使用scribe来完成我使用固定访问令牌的Oauth流程,因此我绕过了OAuth的几个步骤。下面的程序在作为java项目执行时工作正常,但是当我尝试使用android时,它总是响应错误500,服务暂时不可用。 我在两个项目中都包含CommonsCodec以具有类似的编码。 在调试期间,除了响应之外,我得到的每件事都是相同的。

请建议我缺少什么,或者如何以完成相同的替代方式。

package org.scribe.examples;
import java.util.Scanner;

import org.scribe.builder.ServiceBuilder;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.oauth.OAuthService;

/**
 * Example test driver for the Magento17Api class - 
 * Magento Community 1.7.x and Enterprise 1.12 support REST APIs going forward
 * 
 * @see  http://www.magentocommerce.com/blog/the-magento-rest-api-a-better-way-to-integrate-business-applications/
 * @author Mike Salera
 */
public class MagentoExample
{
    public static final String MAGENTO_API_KEY = "MY key";
    public static final String MAGENTO_API_SECRET = "my secret";
    public static final String MAGENTO_REST_API_URL = "http://000.000.0.00/api/rest";

      public static void main(String[] args)
      {
        OAuthService service = new ServiceBuilder()
                                    .provider(MagentoAuth.class)
                                    .apiKey(MAGENTO_API_KEY)
                                    .apiSecret(MAGENTO_API_SECRET)
                                    .debug()
                                    .build();
        Scanner in = new Scanner(System.in);

        System.out.println("=== Mage v1.7.0.2 OAuth Workflow ===");
        System.out.println();

//      // Obtain the Request Token
//      System.out.println("Fetching the Request Token...");
//      Token requestToken = service.getRequestToken();
//      System.out.println("Got the Request Token!");
//      System.out.println();
//
//      System.out.println("Now go and authorize Scribe here:");
//      System.out.println(service.getAuthorizationUrl(requestToken));
//      System.out.println("And paste the verifier here");
//      System.out.print(">>");
//      Verifier verifier = new Verifier(in.nextLine());
//      System.out.println();
//
//      // Trade the Request Token and Verfier for the Access Token
//      System.out.println("Trading the Request Token for an Access Token...");
//      Token accessToken = service.getAccessToken(requestToken, verifier);
        Token accessToken=new Token("hard coded access token", "hard coded secret");
        System.out.println("Got the Access Token!");
        System.out.println("(if your curious it looks like this: " + accessToken + " )");
        System.out.println();

        // Now let's go and ask for a protected resource!
        System.out.println("Now we're going to access a protected resource...");
        OAuthRequest request = new OAuthRequest(Verb.GET, MAGENTO_REST_API_URL + "/customers");
        service.signRequest(accessToken, request);
        Response response = request.send();
        System.out.println("Got it! Lets see what we found...");
        System.out.println();
        System.out.println(response.getBody());

        System.out.println();
        System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
      }
}

1 个答案:

答案 0 :(得分:1)

根据我对划线当前库的研究,Scribe并不意味着在android中使用。最后,我通过使用oauth-signpost library使其工作没有任何问题,并且易于集成。

你需要做的就是包括signpost-commonshttp4-1.2.1.1.jar和signpost-core-1.2.1.2.jar

 public OAuthConsumer getOauthConsumer() {
    OAuthConsumer consumer = new CommonsHttpOAuthConsumer(COUNSUMER_KEY, COUNSUMER_SECRET);
    consumer.setTokenWithSecret(OAUTH_TOKEN, OAUTH_SECRET);
    return consumer;
}

这将为您提供可用于签署请求的OAuthConsumer

HttpPost request=new HttpPost(completeURL);
        getOauthConsumer().sign(request)

就是你完成了。快乐的编码:)