我正在尝试在我的Android应用程序中运行OAuth以从LinkedIn获取用户数据,因为我刚刚启动项目,我遇到了一个主要的错误/问题。从以下代码请求令牌后,令牌未收到,并且应用程序继续在用于身份验证的手机浏览器中打开。 任何帮助都会受到赞赏,因为我对Android开发很新。
package com.tushar.linkedinauth;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.google.code.linkedinapi.client.LinkedInApiClient;
import com.google.code.linkedinapi.client.LinkedInApiClientFactory;
import com.google.code.linkedinapi.client.oauth.LinkedInAccessToken;
import com.google.code.linkedinapi.client.oauth.LinkedInOAuthService;
import com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceFactory;
import com.google.code.linkedinapi.client.oauth.LinkedInRequestToken;
public class MainActivity extends Activity {
public static final String CONSUMER_KEY = "MyConsumerKey";
public static final String CONSUMER_SECRET = "MyConsumerSecret";
public static final String OAUTH_CALLBACK_SCHEME = "x-oauthflow-linkedin";
public static final String OAUTH_CALLBACK_HOST = "callback";
public static final String OAUTH_CALLBACK_URL = OAUTH_CALLBACK_SCHEME + "://" + OAUTH_CALLBACK_HOST;
final LinkedInOAuthService oAuthService = LinkedInOAuthServiceFactory.getInstance().createLinkedInOAuthService(CONSUMER_KEY, CONSUMER_SECRET);
final LinkedInApiClientFactory factory = LinkedInApiClientFactory.newInstance(CONSUMER_KEY, CONSUMER_SECRET);
LinkedInRequestToken liToken;
LinkedInApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new AutheticateLinkedIn().execute();
}
});
Log.d("Token", " Starting Task");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if(liToken!=null)
{
Log.d("Token", "New Intent Recieved");
String verifier = getIntent().getData().getQueryParameter("oauth_verifier");
LinkedInAccessToken accessToken = oAuthService.getOAuthAccessToken(liToken, verifier);
client = factory.createLinkedInApiClient(accessToken);
Log.d("Token", "Posting update");
client.postNetworkUpdate("LinkedIn Android app test");
Log.d("Token", "Update Posted");
}
}
// @Override
// protected void onNewIntent(Intent intent) {
// // TODO Auto-generated method stub
// super.onNewIntent(intent);
//
// }
public class AutheticateLinkedIn extends AsyncTask<Void, Long, Boolean>
{
@Override
protected Boolean doInBackground(Void... params) {
// TODO Auto-generated method stub
Log.d("Token", " Requested");
liToken = oAuthService.getOAuthRequestToken(OAUTH_CALLBACK_URL);
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(liToken.getAuthorizationUrl()));
startActivity(i);
return true;
}
@Override
protected void onPostExecute(Boolean result){
Log.d("Token", " Recieved");
// mDialog.cancel();
}
}
}