簡體   English   中英

在Android上使用Scribe OAuth Java庫時出錯

[英]Error while using Scribe OAuth Java library on android

我在Android上使用Scribe庫獲取Flickr OAuth令牌。

我使用了提供的相同示例

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! :)");
    }

我收到此錯誤-

創建與遠程服務的連接時出現問題。

這里有什么問題 ?

供你參考:

網絡操作不應在主UI線程中使用。 將這些操作作為后台操作進行。

有關更多詳細信息,請參見Android中的AsyncTask 解決了連接網絡的問題。 希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM