抄写的OAuth不工作在Android上,错误500 [英] Scribe OAuth not working on android, error 500

查看:131
本文介绍了抄写的OAuth不工作在Android上,错误500的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是低于code访问Magento的API。我用抄写员来完成,其中我使用固定的访问令牌,所以我绕过的OAuth几步之遥的Oauth过程。下面程序作为Java项目执行时,工作正常,但当我与Android尝试它,它总是出现错误500,服务不可用的临时应对。
在这两个项目中,我已经包括共享codeC也有类似的编码。
在调试期间我收到的每一件事情,除了相同的反应。

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

 包org.scribe.examples;
进口java.util.Scanner中;进口org.scribe.builder.ServiceBuilder;
进口org.scribe.model.OAuthRequest;
进口org.scribe.model.Response;
进口org.scribe.model.Token;
进口org.scribe.model.Verb;
进口org.scribe.oauth.OAuthService;/ **
 *例试车手为Ma​​gento17Api类 -
 * Magento的社区1.7.x和企业支持1.12 REST API的前进
 *
 * @see http://www.magentocommerce.com/blog/the-magento-rest-api-a-better-way-to-integrate-business-applications/
 * @author迈克萨莱拉
 * /
公共类MagentoExample
{
    公共静态最后弦乐MAGENTO_API_KEY =我的钥匙;
    公共静态最后弦乐MAGENTO_API_SECRET =我的秘密;
    公共静态最后弦乐MAGENTO_REST_API_URL =htt​​p://000.000.0.00/api/rest;      公共静态无效的主要(字串[] args)
      {
        OAuthService服务=新ServiceBuilder()
                                    .provider(MagentoAuth.class)
                                    .apiKey(MAGENTO_API_KEY)
                                    .apiSecret(MAGENTO_API_SECRET)
                                    的.debug()
                                    。建立();
        扫描仪在=新的扫描仪(System.in);        的System.out.println(===法师v1.7.0.2 OAuth的认证流程===);
        的System.out.println();// //获取请求令牌
//的System.out.println(读取请求令牌......);
//令牌requestToken = service.getRequestToken();
//的System.out.println(得到了请求令牌!);
//的System.out.println();
//
//的System.out.println(现在去这里抄写授权:);
//的System.out.println(service.getAuthorizationUrl(requestToken));
//的System.out.println(在这里粘贴验证);
// System.out.print(>>中);
//验证验证=新的验证(in.nextLine());
//的System.out.println();
//
// //交易请求令牌和Verfier的访问令牌
//的System.out.println(交易请求令牌访问令牌......);
//令牌的accessToken = service.getAccessToken(requestToken,验证);
        令牌的accessToken =新的令牌(硬codeD访问令牌,硬codeD的秘密);
        的System.out.println(获得了访问令牌!);
        的System.out.println((如果你好奇,它看起来是这样的:+的accessToken +));
        的System.out.println();        //现在让我们去,并要求受保护的资源!
        的System.out.println(现在我们要访问受保护资源...);
        OAuthRequest要求=新OAuthRequest(Verb.GET,MAGENTO_REST_API_URL +/用户);
        service.signRequest(的accessToken,请求);
        响应响应= request.send();
        的System.out.println(!心动不如让我们来看看我们发现了什么......);
        的System.out.println();
        的System.out.println(response.getBody());        的System.out.println();
        的System.out.println(那是它的人去,建设有抄写的东西真棒:)!);
      }
}


解决方案

抄写并不意味着在android系统中使用按我的上划线当前库的研究。最后我得到它通过使用 OAuth的路标图书馆工作它的工作没有任何问题和它的易于集成。

所有你需要做的是包括路标,commonshttp4-1.2.1.1.jar和路标 - 核心1.2.1.2.jar

 公共OAuthConsumer getOauthConsumer(){
    OAuthConsumer消费=新CommonsHttpOAuthConsumer(COUNSUMER_KEY,COUNSUMER_SECRET);
    consumer.setTokenWithSecret(组oauth_token,OAUTH_SECRET);
    返回的消费者;
}

这将为您提供OAuthConsumer可以用来签名的请求。

  HttpPost要求=新HttpPost(completeURL);
        getOauthConsumer()。签名(要求)

这就是它,你就完成了。快乐编码:)

I am using the below code to access a Magento api. I have used scribe to complete the Oauth process in which i am using fixed access token so I bypassed few steps of OAuth. Below program works fine when executing as java project but when I am trying it with android it always respond with error 500, service temporary unavailable. I have included CommonsCodec in both project to have similar encoding. during debug i am getting every thing same except response.

Please suggest what am I missing or, how alternate way to accomplish the same.

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! :)");
      }
}

解决方案

Scribe is not meant to be used in android as per my research on scribe current library. Finally i got it working by using oauth-signpost library it's working without any problem and its easy to integrate.

all you need to do is to include signpost-commonshttp4-1.2.1.1.jar and 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;
}

This will provide you OAuthConsumer that can be used to sign request

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

that's it you are done. Happy coding :)

这篇关于抄写的OAuth不工作在Android上,错误500的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆