Get app guid from app name - via code

江枫思渺然 提交于 2020-01-03 05:09:10

问题


I've and node application mainAppthat running on CF space, in this node application im getting other application name ( that deployed in the same space) and I want to get from it the application guid , How I can do it ?

This is what I've tried ( I try to get all apps in this space and search for specific app from the guid but I got http 401 - unauthorized ,

  1. any idea how can I get from app that deployed to CF the app app guid (suppose I've the app name )

  2. There is a better way to achieve this ?

getAllApps: () => {

        return new Promise((resolve, reject) => {

            rp({
                uri: 'https://' + CF_API + '/v2/apps',
                json: true

            }).then((data) => {
                "use strict";
                console.log("apps data: " + data);
                resolve(data);
            });
        })

回答1:


You have to first get the access token and pass it in the header of the request you have in your question. See the example below that will get the application guid:

var request = require('request-promise');

request({
      "method":"POST",
      "uri": "https://login.ng.bluemix.net/UAALoginServerWAR/oauth/token",
      "json": true,
      "headers": {
        "content-type": "application/x-www-form-urlencoded",
        "authorization": "Basic Y2Y6",
        "accept": "application/json"
      },
      "form" : {
        "grant_type": "password",
        "username": "<your Bluemix id>",
        "password": "<your Bluemix password>"
      }
}).then(function(response) {
      return response.access_token;
}).then(function(token) {
  return request({
    "method":"GET",
    "uri": "https://api.ng.bluemix.net/v2/apps",
    "qs": {
      "q": "name:yourappname"
    },
    "json": true,
    "headers": {
      "accept": "application/json",
      "authorization": "bearer " + token
    }
  }).then(function (response) {
    console.log(response.resources[0].metadata.guid);
  });
});



回答2:


Check this:

http://apidocs.cloudfoundry.org/272/apps/get_app_summary.html

With the httpclient API, you can get the app names from any language.



来源:https://stackoverflow.com/questions/46299829/get-app-guid-from-app-name-via-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!