2016-05-11 6 views
0

私はScribeJava OAuthメカニズムを使用してKhan Academyにアクセスしようとしています。トークンを正常に取得した後は、401の例外が発生します。これは、不正アクセスについてはっきりとしています。私は1OAuth in Java - OAuthエラー。コンシューマアクセスが拒否されました

http://api-explorer.khanacademy.org/api/v1/user/videos/cGg1j1ZCCOs?userId=vilething&username=&email=

にURLを変更した場合、それが私に応答を与えます。しかし、すべてのユーザー関連情報は応答として空です。

public abstract class KhanAcademyAccess { 

    private final static String USER_AGENT = "Mozilla/5.0"; 

    public static void main(String... args) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException, IOException, Exception { 
     String videoId = "C38B33ZywWs"; 
     String username = "###"; 
     String password = "###"; 
     final OAuth10aService service = new ServiceBuilder() 
       .apiKey("###") 
       .apiSecret("###") 
       .build(KAApi.instance()); 

     //Get Request Token 
     final OAuth1RequestToken requestToken = service.getRequestToken(); 

     System.out.println(requestToken.getToken()); 
     sendPost("https://www.khanacademy.org/api/auth2/authorize", username, password, requestToken.getToken().toString()); 
     final OAuth1AccessToken accessToken = service.getAccessToken(requestToken, ""); 
     System.out.println(accessToken); 


     final OAuthRequest request = new OAuthRequest(Verb.GET, "https://www.khanacademy.org/api/internal/_mt/user/videos/cGg1j1ZCCOs/log_compatability?", service); 

     service.signRequest(accessToken, request); 
      request.addHeader("Accept", "application/json; charset=utf-8"); 
      request.addHeader("Content-Type", "application/json; charset=utf-8"); 
      request.addHeader("X-Requested-With", "XMLHttpRequest"); 
      request.addHeader("Accept-Encoding", "application/json"); 

     Response response = request.send(); 

     System.out.println(response.getCode()); 
     System.out.println(response.getMessage()); 
     System.out.println(response.getBody()); 
    } 
private static void sendPost(String url, String username, String password, String oauthToken) throws Exception { 

     DefaultHttpClient httpclient = new DefaultHttpClient(); 
     // Comment out from here (Using /* and */)... 
     // ...to here and the request will fail with "HttpResponseException: Moved Permanently" 
     try { 
      HttpPost httpPost = new HttpPost(url); 
      String encoding = Base64Encoder.getInstance().encode(StringUtils.getBytes(username + ":" + password)); 

      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("identifier", username)); 
      params.add(new BasicNameValuePair("password", password)); 
      params.add(new BasicNameValuePair("oauth_token", oauthToken)); 

      httpPost.setEntity(new UrlEncodedFormEntity(params)); 
      System.out.println("executing request " + httpPost.getURI()); 
      // Create a response handler 
      ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
      HttpResponse responseBody = httpclient.execute(httpPost); 
      // Add your code here... 
      int responseCode = responseBody.getStatusLine().getStatusCode(); 
      while(responseCode!=200){ 
       for(Header header : responseBody.getAllHeaders()){ 
        if(header.getName().equalsIgnoreCase("Location")){ 
         responseCode = getRequest(header.getValue()); 
        } 

       } 

      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      // When HttpClient instance is no longer needed, shut down the connection 
      // manager to ensure immediate deallocation of all system resources 
     } 

    } 

私は承認プロセスを自動化すると、ユーザーがページにリダイレクトされるようにしたいとAcceptボタンをクリックしていないので、私は、ユーザー名とパスワード認証を使用しています。

OAuth1RequestToken {oauth_token = t0000005405940108、 oauth_token_secret = NyGQchdSAj7AwukA、oauth_callback_confirmed = TRUE}

:問題

要求トークンを識別するために有用である可能性があるログを追加

アクセストークン:

OAuth1AccessToken {oauth_token = t0000005413417574、 oauth_token_secret = UMAV7Y54suAVPcJZ}

答えて

0

のOAuthの目的は、ユーザーのユーザー名とパスワードを知っている必要があなたのアプリケーションを避けるためです。 Oauth1では、ブラウザのリダイレクトは標準的な手順です。多くのユーザーがGoogleとFacebookを使ってログインしているため、資格情報を直接使用することはできません。

javaでKhan APIを使用する方法の例については、this example gist

を参照してください。
関連する問題