0

Such a situation , i use scribe-java (https://github.com/fernandezpablo85/scribe-java) to authenticate through Oauth ,and always get such an error:

     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();

Please Help who can or solved such an issue.

  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();

// Now let's go and ask for a protected resource! OAuthRequest request = new OAuthRequest(Verb.GET, MAGENTO_REST_API_URL+ "/products?limit=2"); service.signRequest(accessToken, request); Response response = 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>
  • look at this post http://stackoverflow.com/questions/17865468/scribe-java-and-async-request-in-android-4 answer is there – Andrew Niken Mar 20 '15 at 15:31

2 Answers2

0

In MagentoThreeLeggedOAuth.class, change the BASE_URL to your base url. Instead of using AsyncTask, you can write that code in onCreate() method, and include following lines to your onCreate() method :

StrictMode.ThreadPolicy policy = new  
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);  
Pierre
  • 2,552
  • 5
  • 26
  • 47
KRUPEN GHETIYA
  • 211
  • 2
  • 6
0

I think may be error at OAuthService service, you sure that's contain value of MAGENTO API KEY & MAGENTO API_SECRET exactly. and when registering the application, you also need to define the CALLBACK_URL, to be redirected after successfully authorizes. Ex:

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

refer standard Scribe in java at here : example scribe-java-android
Good luck ^^

mrKOL
  • 11
  • 1