使用 scribe 将 Android 应用程序连接到 API
我正在尝试制作一个与 Bodymedia 数据 API 连接的 Android 应用程序(它是一个可以感知您的身体活动水平的臂带)。我正在使用 scribe oauth 库。我遇到的问题是函数“shouldOverrideUrlLoading”从未被调用。
这是代码:
package com.android.fitapp;
import org.scribe.builder.ServiceBuilder;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.model.Verifier;
import org.scribe.oauth.OAuthService;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
public class VisWeight extends Activity {
final static String API_KEY = "[key]";
final static String API_SECRET = "[secret]";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.oauth);
final OAuthService oauthService = new ServiceBuilder()
.provider(new BodyMediaAPI(API_KEY))
.apiKey(API_KEY)
.apiSecret(API_SECRET)
.build();
final Token requestToken = oauthService.getRequestToken();
final String authURL = oauthService.getAuthorizationUrl(requestToken);
final TextView textView = (TextView)findViewById(R.id.textview);
final WebView webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if(url.startsWith("http")){
webview.setVisibility(View.GONE);
Uri uri = Uri.parse(url);
String verifier = uri.getQueryParameter("oauth_verifier");
Verifier v = new Verifier(verifier);
Token accessToken = oauthService.getAccessToken(requestToken, v);
OAuthRequest req = new OAuthRequest(Verb.GET, "http://api.bodymedia.com/v1.0/goal/current?api_key=" + API_KEY);
oauthService.signRequest(accessToken, req);
Response response = req.send();
textView.setText(response.getBody());
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
});
webview.loadUrl(authURL);
}
}
知道出了什么问题吗?
作为参考,此代码改编自 http 上提供的(可能有效的)代码: //schwiz.net/blog/2011/using-scribe-with-android/,演示了通过 scribe 连接到 twitter API。
I'm trying to make an Android application that connects with the API for Bodymedia data (it's an armband that senses your physical activity levels). I'm using the scribe oauth library. The problem I'm having is that the function "shouldOverrideUrlLoading" is never called.
Here's the code:
package com.android.fitapp;
import org.scribe.builder.ServiceBuilder;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.model.Verifier;
import org.scribe.oauth.OAuthService;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
public class VisWeight extends Activity {
final static String API_KEY = "[key]";
final static String API_SECRET = "[secret]";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.oauth);
final OAuthService oauthService = new ServiceBuilder()
.provider(new BodyMediaAPI(API_KEY))
.apiKey(API_KEY)
.apiSecret(API_SECRET)
.build();
final Token requestToken = oauthService.getRequestToken();
final String authURL = oauthService.getAuthorizationUrl(requestToken);
final TextView textView = (TextView)findViewById(R.id.textview);
final WebView webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if(url.startsWith("http")){
webview.setVisibility(View.GONE);
Uri uri = Uri.parse(url);
String verifier = uri.getQueryParameter("oauth_verifier");
Verifier v = new Verifier(verifier);
Token accessToken = oauthService.getAccessToken(requestToken, v);
OAuthRequest req = new OAuthRequest(Verb.GET, "http://api.bodymedia.com/v1.0/goal/current?api_key=" + API_KEY);
oauthService.signRequest(accessToken, req);
Response response = req.send();
textView.setText(response.getBody());
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
});
webview.loadUrl(authURL);
}
}
Any idea what's going wrong?
For reference, this code was adapted from (the presumably working) code provided on http://schwiz.net/blog/2011/using-scribe-with-android/, which demonstrated connecting to the twitter API via scribe.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
想通了。问题是我没有启用 Javascript。运行后有一些小问题需要修复。如果您想查看完整的更正代码,请随时给我留言。
Figured it out. The problem was that I hadn't enabled Javascript. There were some minor things to fix once that was running. Feel free to message me if you'd like to see the complete corrected code.