【问题标题】:Android scribe-java doesnt provide token for magentoAndroid scribe-java 不为 magento 提供令牌
【发布时间】:2015-05-15 06:14:07
【问题描述】:

这种情况,我使用 scribe-java (https://github.com/fernandezpablo85/scribe-java) 通过 Oauth 进行认证,总是报这样的错误:

     org.scribe.exceptions.OAuthConnectionException: There was a problem while creating a connection to the remote service.
on this line of code 

        Token requestToken = service.getRequestToken();

请帮助谁能解决或解决此类问题。

  Here my main activity :
    public class MainActivity extends Activity {
            final String MAGENTO_API_KEY = "key";
            final String MAGENTO_API_SECRET = "secret";
            final String MAGENTO_REST_API_URL = "https://www.myweb.com/api/rest";

        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        doAsyncTask doIt = new doAsyncTask();
            doIt.doInBackground();
    }
    public void testOauth(){
        OAuthService service = new ServiceBuilder()
                .provider(MagentoThreeLeggedOAuth.class)
                .apiKey(MAGENTO_API_KEY)
                .apiSecret(MAGENTO_API_SECRET)
                .callback("SUCCESS")
                .debug()
                .build();
        Scanner in = new Scanner(System.in);
        System.out.println("Magento'srkflow");
        System.out.println();
        // Obtain the Request Token
        System.out.println("FetchingRequest Token...");
        Token requestToken = service.getRequestToken();
        System.out.println("GotRequest Token!");
        System.out.println();

        // Obtain the Authorization URL
        System.out.println("FetchingAuthorization URL...");
        String authorizationUrl = service.getAuthorizationUrl(requestToken);
        System.out.println("GotAuthorization URL!");
        System.out.println("Nownd authorize Main here:");
        System.out.println(authorizationUrl);
        System.out.println("Ande the authorization code 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("TradingRequest Token for an Access Token...");
        Token accessToken = service.getAccessToken(requestToken, verifier);
        System.out.println("GotAccess Token!");
        System.out.println("(if curious it looks like this: "
                + accessToken + " )");
        System.out.println();

// 现在让我们去请求一个受保护的资源! OAuthRequest 请求 = 新 OAuthRequest(Verb.GET, MAGENTO_REST_API_URL+ "/products?limit=2"); service.signRequest(accessToken, request); 响应响应 = request.send(); System.out.println(); System.out.println(response.getCode()); System.out.println(response.getBody()); System.out.println();

     /*   OAuthRequest request = new OAuthRequest(Verb.POST, MAGENTO_REST_API_URL+"/customers");
        request.addHeader("Content_Type", "text/xml");//this is a nasty bug in the current Magento implementation...we have to sent over
        final String user = "<!--?xml version=\"1.0\"?-->" +
                "<magento_api>" +
                "<firstname>Gerardo</firstname>" +
                "<lastname>Martinez</lastname>" +
                "<password>123123q</password>" +
                "<email>jerry@example.com</email"+
        "<website_id>1</website_id>" +
                "<group_id>1</group_id>" +
                "</magento_api>";
        request.addPayload(user);
        service.signRequest(accessToken, request);
        Response response = request.send();
        System.out.println();
        System.out.println(response.getCode());
        System.out.println(response.getBody());*/

    }
    public class doAsyncTask extends AsyncTask<String,Void,Boolean>
        @Override
        protected Boolean doInBackground(String... params) {

            testOauth();

            return null;
        }
    }

    public static final class MagentoThreeLeggedOAuth extends DefaultApi10a {
        private static final String BASE_URL = "https://www.myweb.com/";

        @Override
        public String getRequestTokenEndpoint() {
            return BASE_URL + "oauth/initiate";
        }

        @Override
        public String getAccessTokenEndpoint() {
            return BASE_URL + "oauth/token";
        }

        @Override
        public String getAuthorizationUrl(Token requestToken) {
            return BASE_URL + "admin/oauth_authorize?oauth_token="
                    + requestToken.getToken(); //this implementation is for admin roles only...
        }

        }

    }

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.example.myapplication" >

    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

【问题讨论】:

标签: java android rest magento scribe


【解决方案1】:

MagentoThreeLeggedOAuth.class 中,将BASE_URL 更改为您的基本网址。 除了使用 AsyncTask,您可以在 onCreate() 方法中编写该代码,并在您的 onCreate() 方法中包含以下几行:

StrictMode.ThreadPolicy policy = new  
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);  

【讨论】:

    【解决方案2】:

    我认为OAuthService 服务 可能有错误,您确定其中包含MAGENTO API KEYMAGENTO API_SECRET 的值。 并且在注册应用时,还需要定义CALLBACK_URL,授权成功后重定向。 例如:

    service = new ServiceBuilder()
                .provider(MagentoThreeLeggedOAuth.class)
                .apiKey(Contants.ConsumerKey)
                .apiSecret(Contants.ConsumerSecret)
                .callback(Contants.URL_CALLBACK)
                .build();
    

    在此处参考 java 中的标准 Scribe:example scribe-java-android
    祝你好运^^

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-31
      • 2018-01-31
      • 2019-06-23
      • 2014-03-17
      相关资源
      最近更新 更多