gpt4 book ai didi

java - 在 Android 上将 OAuth 与 Scribe 结合使用

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:40:39 26 4
gpt4 key购买 nike

您好,我正在使用 scribe 库与 LInkedIn 进行 OAuth 通信。

我有一个登录类和一个查询类。

登录类创建一个服务来获取我的请求 token 和密码,并创建我的访问 token 。然后将访问 token 保存到首选项文件中。这似乎工作正常,我可以在创建所有 token 后成功调用 api。

在我的 OAuth 查询类中,我检索访问 token ,创建另一个服务并尝试进行 API 调用,但是每当我加载一个调用此类的 Activity 时,它都会导致我的应用程序崩溃。我已经过测试以确保访问 token 已保存并且它们已保存。

这是我的登录类

public class Login_LinkedIn extends Activity 
{
SharedPreferences settings;
OAuthService service;
Token requestToken;

Button home;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.webauth);

initControls();

service = new ServiceBuilder()
.provider(LinkedInApi.class)
.apiKey( getString(R.string.apiKey) )
.apiSecret( getString(R.string.secKey) )
.callback( getString(R.string.callBack) )
.build();

requestToken = service.getRequestToken();
final String authURL = service.getAuthorizationUrl(requestToken);

final WebView webview = (WebView) findViewById(R.id.webView);

//attach WebViewClient to intercept the callback url
webview.setWebViewClient(new WebViewClient()
{

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{

//check for our custom callback protocol
//otherwise use default behavior
if(url.startsWith( getString(R.string.callBack) ))
{
//authorization complete hide webview for now.
webview.setVisibility(View.GONE);
Uri uri = Uri.parse(url);
String verifier = uri.getQueryParameter("oauth_verifier");
Verifier v = new Verifier(verifier);

//save this token for practical use.
Token accessToken = service.getAccessToken(requestToken, v);

OAuthRequest request = new OAuthRequest(Verb.GET, "http://api.linkedin.com/v1/people/~:(first-name,last-name)");
service.signRequest(accessToken, request);
Response response = request.send();

xmlHandler xh = new xmlHandler(response.getBody());

settings = getSharedPreferences("preferences", 0);
SharedPreferences.Editor editor = settings.edit();

editor.putString("accessToken", accessToken.getToken());

// The requestToken is saved for use later on to verify the OAuth request.
// See onResume() below
editor.putString("requestToken", requestToken.getToken());
editor.putString("requestSecret", requestToken.getSecret());

editor.putString("first-name", xh.getValue("first-name"));
editor.putString("last-name", xh.getValue("last-name"));

editor.commit();

return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
});

//send user to authorization page
webview.loadUrl(authURL);
}

@Override
protected void onResume()
{
super.onResume();

Intent i = getIntent();

if(i != null)
{
Uri uri = i.getData();
if(uri != null)
{
String oauthVerifier = uri.getQueryParameter("oauth_verifier");

Verifier verifier = new Verifier(oauthVerifier);

requestToken = new Token(settings.getString("requestToken", null), settings.getString("requestSecret", null));

Token accessToken = service.getAccessToken(requestToken, verifier);

// Save the access token.
SharedPreferences.Editor editor = settings.edit();
editor.remove("requestToken");
editor.remove("requestSecret");
editor.putString("accessToken", accessToken.getToken());
editor.putString("accessSecret", accessToken.getSecret());
editor.commit();

// Start the film list activity.
final Intent intent = new Intent(this,ProConnect.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
}

private void initControls()
{
home = (Button)findViewById(R.id.home);

final Intent intent = new Intent(this,ProConnect.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

home.setOnClickListener(new Button.OnClickListener()
{
public void onClick (View v)
{
startActivity(intent);
}
});


}

}

和我的查询类

public class oAuthQuery extends Activity
{
OAuthService service;
Token accessToken;
SharedPreferences settings;

public oAuthQuery()
{
service= new ServiceBuilder()
.provider(LinkedInApi.class)
.apiKey( getString(R.string.apiKey) )
.apiSecret( getString(R.string.secKey) )
.callback( getString(R.string.callBack) )
.build();

settings = getSharedPreferences("preferences", 0);

accessToken = new Token(settings.getString("accessToken", null), settings.getString("accessSecret", null));

}

public String query(String s)
{
OAuthRequest request = new OAuthRequest(Verb.GET, s);
service.signRequest(accessToken, request);
Response response = request.send();

return response.getBody();
}

public OAuthService getService()
{
return service;
}

}

感谢您的帮助杰夫

最佳答案

我解决了这个问题,它最终是一些愚蠢的事情。我已经编辑了我的代码以保存访问密码和访问 token ,但是在我的手机上测试我的应用程序时忘记重新登录。这导致无法访问保存 token 部分的代码。

我在使用 LinkedIn API 中的邀请调用时仍然遇到问题

invite.setOnClickListener(new Button.OnClickListener() 
{
public void onClick (View v)
{
inviteXml = inviteCreator.inviteString(to, subj, body, authName, authValue);
nameField.setText(inviteXml);
titleField.setText("");


call = "http://api.linkedin.com/v1/people/~/mailbox";
request = new OAuthRequest(Verb.GET, call);
request.addPayload(inviteXml);
service.signRequest(accessToken, request);
response = request.send();

invite.setVisibility(View.GONE);
}
});

我不确定这是否是将 XML 字符串添加到调用的正确方法。 LinkedIn API 没有指定如何添加。有没有人有这方面的经验?

关于java - 在 Android 上将 OAuth 与 Scribe 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6919282/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com