这种情况,我使用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 request = new 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>
答案 0 :(得分:0)
在MagentoThreeLeggedOAuth.class
中,将BASE_URL
更改为您的基本网址。
您可以使用onCreate()
方法编写该代码,而不是使用AsyncTask,并在onCreate()
方法中包含以下行:
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
答案 1 :(得分:0)
我认为 OAuthService服务可能有误,您确定其中包含 MAGENTO API KEY &amp;完全 MAGENTO 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
祝你好运^^