Get app guid from app name - via code

2019-06-10 06:52发布

问题:

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.