2016-04-06 10 views
0

Ben yetkisiz duyuyorum bir hata alıyorum .. TEPKİ KOD 401
belirteç ben perl'de eserlerini kullanıyorum ..Java'da oauth2 çalışması için perl kodu yapmak ister "curl -H 'Yetkilendirme: Taşıyıcı: <TOKEN> https://canvas.instructure.com/api/v1";

Şu ana kadar denedim budur:

public static void main(String[] args) throws Exception { 
     try { 
     String auth = returnAuth(); //getting token from a file. 
     //System.out.println(auth); 
     String url1= "https://canvas.instructure.com/api/v1"; 
     URL url = new URL(url1+"/courses"); 
     HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); 
     connection.setRequestMethod("GET");  

     //connection.setRequestProperty("Authorization", "Bearer " + auth); 

     connection.setRequestProperty("Authorization", "Bearer "+auth); 
     System.out.println("\nSending 'GET' request to URL : " + url); 
     System.out.println("Response code:" + connection.getResponseCode()); 
     System.out.println("Response message:" + connection.getResponseMessage()); 

      // Read the response: 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
      connection.getInputStream())); 
      String line; 
      StringBuffer response = new StringBuffer(); 
      while ((line = reader.readLine()) != null) { 
       response.append(line); 
      } 
      reader.close(); 
      System.out.println(response.toString()); 
     } 
     catch (MalformedURLException e1) { 
      e1.printStackTrace(); 
     } catch (IOException e2) { 
      e2.printStackTrace();   
     } 
    } 

cevap

1
public static void main(String[] args) throws Exception { 
    try { 
    String auth = returnAuth(); //getting token from a file. 
    String url1= "https://canvas.instructure.com/api/v1/courses"; 
    URL url = new URL(url1); 
    HttpsURLConnection connection =(HttpsURLConnection) url.openConnection(); 
    connection.setRequestMethod("GET");  

    connection.setRequestProperty("Authorization", "Bearer "+auth); 
    System.out.println("\nSending 'GET' request to URL : " + url); 
    System.out.println("Response code:" + connection.getResponseCode()); 
    System.out.println("Response message:" + connection.getResponseMessage()); 

     // Read the response: 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
     connection.getInputStream())); 
     String line; 
     while ((line = reader.readLine()) != null) { 
      System.out.println(line); 
     } 
     reader.close(); 
    } 
    catch (MalformedURLException e1) { 
     e1.printStackTrace(); 
    } catch (IOException e2) { 
     e2.printStackTrace();   
    } 
} 
0

You have yorumladı, ancak ele aldığım her Oauth sunucusu "Bearer <jetonu>" - bir boşluk ve kolon olmaz.

+0

o hata için üzgünüm. Ama kolon olmadan ve bir boşlukla denedim .. hala çalışmıyor – PrasadPatil

+0

Simgesini nasıl oluşturduğunuzu sormalıyım - sınırlı bir ömrü vardır ve geçerli olmaları gerekir. İlk önce auth'u almak için Postman (https://www.getpostman.com/) Firebug (https://getfirebug.com/) gibi bir şey kullanmanızı önerebilirim ve * sonra Java kodunu çalıştırabilirim. – stdunbar

+0

Yeni bir belirteç oluşturup çalışıp çalışmadığınızı ve öneri üzerinde çalışmayı deneyeceğim. Teşekkürler. – PrasadPatil

0

Parametreleri, aradığınızdan sonra ayarlayabilirsiniz!

Kodunuzu, önce kimlik doğrulamayı ayarlamak için değiştirin, ardından bağlantıyı açın.

String url1= "https://canvas.instructure.com/api/v1"; 
URL url = new URL(url1+"/courses"); 
connection.setRequestProperty("Authorization", "Bearer "+auth); 
connection.setRequestMethod("GET");  

//now you can open the connection... 

HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); 
+0

Yöntem returnAuth(), Token Düzgün olarak dönmedi. Açmadan önce, İstekte bulunmadan önce. – PrasadPatil

İlgili konular