gpt4 book ai didi

android - Scribe OAuth 无法在 Android 上运行,错误 500

转载 作者:行者123 更新时间:2023-11-29 01:42:12 27 4
gpt4 key购买 nike

我正在使用以下代码访问 Magento api。我已经使用 scribe 来完成我使用固定访问 token 的 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! :)");
}
}

最佳答案

根据我对 scribe 当前库的研究,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)

就这样你就完成了。快乐编码:)

关于android - Scribe OAuth 无法在 Android 上运行,错误 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23803957/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com