我正在尝试使用路标从Android的Magento Oauth获取访问令牌。为此,我使用了以下代码。
final String MAGENTO_API_KEY = "xxxxxxxxxxxxxxxxxxx";
final String MAGENTO_API_SECRET = "xxxxxxxxxxxxxxxxxxxxx";
final String OAUTH_INIT_URL = "http://ip/magento/oauth/initiate/";
final String ACCESS_TOKEN_URL = "http://ip/magento/oauth/token/";
final String AUTHORIZE_URL = "http://ip/magento/admin/oAuth_authorize/";
CommonsHttpOAuthConsumer consumer;
CommonsHttpOAuthProvider provider;
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if( Build.VERSION.SDK_INT >= 9){
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
setContentView(R.layout.activity_main);
new OauthConnectAsync().execute();
}
public class OauthConnectAsync extends AsyncTask <String, Void, String>{
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
consumer = new CommonsHttpOAuthConsumer(MAGENTO_API_KEY, MAGENTO_API_SECRET);
provider = new CommonsHttpOAuthProvider(OAUTH_INIT_URL,ACCESS_TOKEN_URL, AUTHORIZE_URL);
provider.setOAuth10a(true);
provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);
} catch (OAuthMessageSignerException | OAuthNotAuthorizedException
| OAuthExpectationFailedException
| OAuthCommunicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("Tokens" , consumer.getToken() + " -- " + consumer.getTokenSecret());
return null;
}
protected void onPostExecute(String file_url) {
}
但代码给出了404未找到的错误,如下所示。
08-11 10:10:27.692: W/System.err(1860): oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: Service provider responded in error: 404 (Not Found)
当我使用网址时
我的Firefox RESTClient中的http://localhost/magento/oauth/initiate
得到了响应As
oauth_problem=parameter_absent&oauth_parameters_absent=oauth_consumer_key
我在Android代码中找不到错误,我真的很感激任何帮助,我在最近几天坚持这一点。如果我需要改变什么,请告诉我。