我正在使用 scribe 库向 Magento 注册 android。
但是我收到错误:
org.scribe.exceptions.OAuthException: Response body is incorrect. Can't extract token and secret from this: '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<p>The requested URL /oauth/initiate was not found on this server.</p>
但是我的密钥、秘密和网址都是正确的。我正确定义了用户和角色。
我从这里参考:https://gmartinezgil.wordpress.com/2013/08/05/using-the-magento-rest-api-in-java-with-scribe/
我的代码是这样的: 从 Activity 调用 Asyntask:
new OauthAsyncTask().execute();
然后我的任务是:
public class OauthAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
// oauth for magento api access
OAuthService service = new ServiceBuilder()
.provider(MagentoThreeLeggedOAuth.class)
.apiKey(MAGENTO_API_KEY)
.apiSecret(MAGENTO_API_SECRET)
.build();
Token requestToken = service.getRequestToken();
String authorizationUrl = service.getAuthorizationUrl(requestToken);
Verifier verifier = new Verifier("Getting TOken");
Log.e("authorizationUrl:", authorizationUrl);
Token accessToken = service.getAccessToken(requestToken, verifier);
Log.e("accessToken:", accessToken.toString());
OAuthRequest request = new OAuthRequest(Verb.GET, "MAGENTO_REST_API_URL"+ "/products");
service.signRequest(accessToken, request);
Response response = request.send();
Log.e("response:", response.toString());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
// 授权类
public static final class MagentoThreeLeggedOAuth extends DefaultApi10a {
private static final String BASE_URL = "http://myapp.com/";
@Override
public String getRequestTokenEndpoint() {
return BASE_URL + "oauth/initiate";
}
@Override
public String getAccessTokenEndpoint() {
return BASE_URL + "oauth/token";
}
@Override
public String getAuthorizationUrl(Token token) {
return null;
}
}
请帮我解决这个问题。
问题很愚蠢但很棘手,我问我的 Magento 开发人员 BASE URL 是什么,他回答说“http://myapp.com/”并陷入了上述问题,当我更多地搜索它时,我发现有些用户使用 BASE URL 例如“http://myapp.com/magento”或“http://myapp.com/magento/index.php”等。所以我发现真正的路径是“http://myapp.com/index.php”,它没有定向到BASE URL。因此,有时当产品处于开发模式时,会发生此类问题,只需与 magento dev 确认确切的路径即可。