android 的 Magento oauth
Magento oauth with android
我正在使用 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>
但我的密钥、秘密和 url 都是正确的。我正确定义了用户和角色。
我参考了here: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;
}
}
// 授权 class
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/" and got stuck with above problem, when I searched about it more, I found that some users using BASE URL like "http://myapp.com/magento" or "http://myapp.com/magento/index.php" etc. So I found that the real path was "http://myapp.com/index.php”,它不是指向 BASE URL。所以有时当产品处于开发模式时会出现这类问题,只需与 magento dev 确认确切路径即可。
我正在使用 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>
但我的密钥、秘密和 url 都是正确的。我正确定义了用户和角色。
我参考了here: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;
}
}
// 授权 class
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/" and got stuck with above problem, when I searched about it more, I found that some users using BASE URL like "http://myapp.com/magento" or "http://myapp.com/magento/index.php" etc. So I found that the real path was "http://myapp.com/index.php”,它不是指向 BASE URL。所以有时当产品处于开发模式时会出现这类问题,只需与 magento dev 确认确切路径即可。