java - 在 Android 上使用 Scribe OAuth Java 库时出错

标签 java android flickr scribe

我在 Android 上使用 Scribe 库来获取 Flickr OAuth token 。

我使用了

中提供的相同示例

https://github.com/fernandezpablo85/scribe-java/blob/master/src/test/java/org/scribe/examples/FlickrExample.java

将 System.out.println 替换为 Log.d。

 private static final String PROTECTED_RESOURCE_URL = "https://api.flickr.com/services/rest/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Replace these with your own api key and secret
        String apiKey = "xxxx989907fxxxxxxxxxxxxxxxxxxxx";
        String apiSecret = "fbxxxxx7dxxxxx8";
        OAuthService service = new ServiceBuilder().provider(FlickrApi.class).apiKey(apiKey).apiSecret(apiSecret).build();
        Scanner in = new Scanner(System.in);

        Log.d("","=== Flickr's OAuth Workflow ===");
        System.out.println();

        // Obtain the Request Token
        Log.d("", "Fetching the Request Token...");
        Token requestToken = service.getRequestToken();
        Log.d("", "Got the Request Token!");
        System.out.println();

        Log.d("", "Now go and authorize Scribe here:");
        String authorizationUrl = service.getAuthorizationUrl(requestToken);
        Log.d("", authorizationUrl + "&perms=read");
        Log.d("", "And paste the verifier here");
        Log.d("", ">>");
        Verifier verifier = new Verifier(in.nextLine());
        System.out.println();

        // Trade the Request Token and Verfier for the Access Token
        Log.d("", "Trading the Request Token for an Access Token...");
        Token accessToken = service.getAccessToken(requestToken, verifier);
        Log.d("", "Got the Access Token!");
        Log.d("", "(if your curious it looks like this: " + accessToken + " )");
        Log.d("", "(you can get the username, full name, and nsid by parsing the rawResponse: " + accessToken.getRawResponse() + ")");
        System.out.println();

        // Now let's go and ask for a protected resource!
        Log.d("", "Now we're going to access a protected resource...");
        OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
        request.addQuerystringParameter("method", "flickr.test.login");
        service.signRequest(accessToken, request);
        Response response = request.send();
        Log.d("", "Got it! Lets see what we found...");
        System.out.println();
        Log.d("", response.getBody());

        System.out.println();
        Log.d("", "Thats it man! Go and build something awesome with Scribe! :)");
    }

我收到此错误 -

There was a problem while creating a connection to the remote service.

这里有什么问题吗?

最佳答案

供您引用:

网络操作不应在主 UI 线程中使用。将这些操作作为后台操作进行。

有关更多详细信息,请参阅 Android 中的 AsyncTask。它解决了连接网络问题。希望这会有所帮助。

关于java - 在 Android 上使用 Scribe OAuth Java 库时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32087087/

相关文章:

python - Trickle 无法调整我的 Python 脚本的上传带宽

ios - 数据乱序

java - 使用 Jersey 是否可以隐藏某些类别属性?

java - 在列表中添加假类型时没有异常

android - 使用什么样的数据库?

Android 支持库 v4 对话框 fragment

java - 带有 Sentinel 的 Redisson 客户端抛出异常

java - Weblogic 数据源从 JNDI 树中消失

android - 更快地截取 View 的方法

javascript - 如何在 javascript 中以 unix 格式获取昨天的日期?