【问题标题】:Can't convert Apigee Java Object response to string无法将 Apigee Java 对象响应转换为字符串
【发布时间】:2014-05-21 06:31:06
【问题描述】:

我正在使用 GetAPIProduct 策略(请参阅 http://apigee.com/docs/gateway-services/content/retrieve-api-product-settings-using-getapiproduct ) 以获取范围列表。然后在 JavaScript 标注中,我尝试引用该范围列表,但我得到的不是文本,而是这样的内容(最后的十六进制块随着每次调用而变化):

 [Ljava.lang.String;@19baa7ed

我似乎无法将其转换为可以使用 Javascript 访问的数组。我在一个免费组织中,所以 Java 不是一个选项。我已经尝试过 String()、myvar.toString() 甚至是 Apigee 中似乎不存在的 Rhino context.javaToJs。

有什么办法可以将其转换为字符串吗?

【问题讨论】:

    标签: java apigee


    【解决方案1】:

    原来getapiproduct.{policyname}.apiproduct.scopes 的值是一个从零开始的对象数组。但是,这些对象中的每一个都可以转换为作为范围名称的字符串。

    以下是访问范围数组的方法:

    var scopeArray=context.getVariable("getapiproduct.RetrieveProductInfo.apiproduct.scopes");
    
    // you can use either of these methods to convert the array elements
    var firstElement = String(scopeArray[0]);
    var secondElement = scopeArray[1]+'';
    
    var scopeArrayLen = scopeArray.length;
    

    【讨论】:

      【解决方案2】:

      这不是问题的答案,而是一些可能有价值的附加信息。

      GetAPIPRoductInfo 获取产品的范围列表。相反,GetOAuthV2Info 策略获取有关 OAUthV2 令牌的信息。假设你有一个令牌,你可以这样做:

      <GetOAuthV2Info name='GetOAuthV2Info-TokenScopes'>
        <!-- use one of the following: a referenced variable or -->
        <!-- an explicitly passed access_token -->
        <AccessToken ref='access_token'/>
      
        <!--
            On Success, the following flow variables will be set.
            oauthv2accesstoken.{policy_name}.access_token
            oauthv2accesstoken.{policy_name}.scope
            oauthv2accesstoken.{policy_name}.refresh_token
            oauthv2accesstoken.{policy_name}.accesstoken.{custom_attribute_name}
            oauthv2accesstoken.{policy_name}.developer.id
            oauthv2accesstoken.{policy_name}.developer.app.name
            oauthv2accesstoken.{policy_name}.expires_in
            oauthv2accesstoken.{policy_name}.status
        -->
      </GetOAuthV2Info>
      

      然后,您可以在后续的 JS 标注中使用该信息来检查令牌的范围是否符合您的任何要求:

      // checkScope.js
      // ------------------------------------------------------------------
      
      var varname = 'oauthv2accesstoken.GetOAuthV2Info-TokenScopes.scope',
          approvedScopes = context.getVariable(varname),
          check = false;
      
      approvedScopes = approvedScopes.split(' ');
      // approvedScopes is now a JavaScript array of strings, that lists
      // the scopes the user approved for the requesting client (app).
      //
      // You can now compare that list against the scopes required
      // for an operation or resource, and then set a variable 
      // determining whether the token is good for the request. 
      
      context.setVariable('scopeCheck.ok', check);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-02
        • 2013-06-28
        • 1970-01-01
        • 2012-01-05
        • 2013-08-02
        • 1970-01-01
        相关资源
        最近更新 更多