私はアンドロイドにかなり慣れていません、どんな間違いも許してください。
package net.schwiz.oauth;
import org.json.JSONException;
import org.json.JSONObject;
import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.TwitterApi;
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 Main extends Activity {
final static String APIKEY = "XXXXXXXXXXXXXXXXXXXX";
final static String APISECRET = "XXXXXXXXXXXXXXXXXXXX";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textView = (TextView)findViewById(R.id.textview);
final WebView webview = (WebView) findViewById(R.id.webview);
if(APIKEY == null || APISECRET == null){
textView.setText("You must enter your own APIKEY and SECRET to use this demo. dev.twitter.com");
webview.setVisibility(View.GONE);
return;
}
//set up service and get request token as seen on scribe website
//https://github.com/fernandezpablo85/scribe-java/wiki/Getting-Started
final OAuthService s = new ServiceBuilder()
.provider(TwitterApi.class)
.apiKey(APIKEY)
.apiSecret(APISECRET)
.callback(CALLBACK)
.build();
final Token requestToken = s.getRequestToken();
final String authURL = s.getAuthorizationUrl(requestToken);
//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("oauth")){
//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 = s.getAccessToken(requestToken, v);
if(uri.getHost().equals("twitter")){
OAuthRequest req = new OAuthRequest(Verb.GET, "URL here");
s.signRequest(accessToken, req);
Response response = req.send();
try {
JSONObject json = new JSONObject(response.getBody());
textView.setText(json.toString(3));
} catch (JSONException e) {
e.printStackTrace();
}
}
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
});
//send user to authorization page
webview.loadUrl(authURL);
}
}
このコードを試しましたが、Twitterで正常に機能していますが、Flickrを使用してユーザーを認証したいのですが、その場合、上記のコードは機能しません。if条件if(url.startsWith("oauth"))
が想定どおりに機能しません。助けてください。