2015-09-09 34 views
1

我试图用这个curl命令的逻辑:如何从fiware实验室获取令牌以访问放置在PeP代理后面的上下文代理?

curl -s --insecure -i --header ${AUTH_BASIC} --header ${CONTENT_TYPE} -X POST https://idm/oauth2/token -d ${DATA}" 
    XAUTH_TOKEN="$(eval ${REQUEST} | grep -Po '(?<="access_token": ")[^"]*')" 
    echo "X-Auth-Token for '${_user}': ${XAUTH_TOKEN} 

写在C#中的要求:

//GETTING TOKEN... 
      String input2 = "'grant_type=password&username=<MyUsername on Lab .fiware.org&password=<myPassword>&client_id=<myClientID>&client_secret=<myClientSecret>'"; 
      var httpWebRequest2 = (HttpWebRequest)WebRequest.Create("https://account.lab.fiware.org/oauth2/token"); 
      httpWebRequest2.ContentType = "application/x-www-form-urlencoded"; 
      //httpWebRequest2.Accept = "application/json"; 
      string authInfo = "0555996e09f340d08a4baa8fff94f8c4" + ":" + "a62333f7045b4ab797669c28f9d26d30"; 
      authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)); 
      httpWebRequest2.Headers["Authorization"] = "Basic " + authInfo; 
      httpWebRequest2.Method = "POST"; 

      using (var streamWriter = new StreamWriter(httpWebRequest2.GetRequestStream())) 
      { 
       streamWriter.Write(input2); 
       streamWriter.Flush(); 
       streamWriter.Close(); 
      } 

      var httpResponse2 = (HttpWebResponse)httpWebRequest2.GetResponse(); 
      using (var streamReader = new StreamReader(httpResponse2.GetResponseStream())) 
      { 
       string result = streamReader.ReadToEnd(); 
       Console.WriteLine(result); 
      } 

,但我得到了以下错误:

enter image description here

当我尝试在localhost中执行这个解决方案时,就像在post here中那样工作没有问题。这可能与我在实验室帐户中的localhost:1307下注册应用程序有关吗?

回答

2

没有足够的细节来调试它,但KeyRock返回400当请求没有很好地形成时。你应该得到KeyRock返回的消息。不过,随着这一要求,就可以得到代码400如果:

  • Authorization: Basic头丢失,在这种情况下,您将获得:

    HTTP/1.1 400 BAD REQUEST 
    Date: Thu, 10 Sep 2015 08:43:25 GMT 
    Server: Apache/2.4.7 (Ubuntu) 
    Vary: Accept-Language,Cookie 
    X-Frame-Options: SAMEORIGIN 
    Content-Language: en 
    Connection: close 
    Transfer-Encoding: chunked 
    Content-Type: text/html; charset=utf-8 
    
    Authentication header missing. Use HTTP Basic. 
    
  • 请求体(即你的input2)是不被发送,在这种情况下,您将获得:

    HTTP/1.1 400 Bad Request 
    Date: Thu, 10 Sep 2015 08:47:49 GMT 
    Server: Apache/2.4.7 (Ubuntu) 
    Vary: Accept-Language,Cookie 
    X-Frame-Options: SAMEORIGIN 
    Content-Language: en 
    Connection: close 
    Transfer-Encoding: chunked 
    Content-Type: application/json 
    
    {"error": {"message": "create_access_token() takes exactly 3 arguments (2 given)", "code": 400, "title": "Bad Request"}} 
    
  • 没有定义grant_type在您的请求主体:

    HTTP/1.1 400 Bad Request 
    Date: Thu, 10 Sep 2015 08:52:58 GMT 
    Server: Apache/2.4.7 (Ubuntu) 
    Vary: Accept-Language,Cookie 
    X-Frame-Options: SAMEORIGIN 
    Content-Language: en 
    Connection: close 
    Transfer-Encoding: chunked 
    Content-Type: application/json 
    
    {"error": {"message": "grant_type missing in request body: {}", "code": 400, "title": "Bad Request"}} 
    
+0

非常好,我们将看到堆栈跟踪说什么。同时你有没有机会知道令牌会话的长度?我们从运行pep代理的机器本地生成令牌,并将其转发到我们运行C#代码的机器上,并且它可以正常工作。这已经超过24小时,因为我们生成了令牌,并且我们想知道它何时到期? – Vrankela

+0

长度取决于您想要的令牌长度或“内容长度”。默认情况下,令牌'“expires_in”:3600' – albertinisg

+0

我的意思是它何时到期。当令牌过期时,我没有触及任何配置,这意味着我应该在一小时后不能使用相同的令牌。然而在这里我使用的是24小时前生成的令牌。 原来的问题,我们有麻烦产生错误诊断问题,我们只能产生: HTTP/1.1 400错误的请求 日期:星期四,2015年9月10日8时52分58秒GMT 服务器:Apache /2.4.7(Ubuntu的) 各不相同:接受语言,饼干 X框选项:SAMEORIGIN 内容语言:zh 连接:关闭 传输编码:分块 内容类型:应用程序/ JSON – Vrankela

相关问题