如何从CloudFoundry获取OAuth令牌

时间:2015-01-16 13:52:11

标签: oauth-2.0 cloudfoundry

我想使用curl从云代工厂获取数据,但我无法进行身份验证(针对CF的oauth)。请问,有人能指出我并举例说明如何获得oauth令牌吗?我想使用登录名和密码。

由于

4 个答案:

答案 0 :(得分:15)

这并不是您要求的,但如果安装了最近的cf cli,您可以正常登录,然后使用cf curl命令运行原始请求。

例如

$ cf login (or cf auth for non-interactive login)
$ cf curl /v2/spaces/c4e73f65-4dbc-47dc-9d21-e8c566c40587/summary

要使用实际curl,再次使用cf cli,请使用以下内容检索持票人令牌:

$ cf oauth-token

然后使用Authorization标头执行curl命令:

$ curl --header 'Authorization: bearer ...' https://api.example.com/v2/spaces/c4e73f65-4dbc-47dc-9d21-e8c566c40587/summary

答案 1 :(得分:3)

试试这个。我在Cloud Foundry文档中找到了它:https://github.com/cloudfoundry/uaa/blob/master/docs/UAA-APIs.rst#oauth2-token-endpoint

curl -v -XPOST -H"Application/json" -u "cf:" --data "username=<username>&password=<password>&client_id=cf&grant_type=password&response_type=token" https://login.run.pivotal.io/oauth/token

答案 2 :(得分:2)

如果您已使用 $http .get( '/data/?some=parameters&go=here' ) .success( function( response, status, headers ) { $scope.headers['cache-control'] = headers('cache-control'); $scope.headers['connection'] = headers('connection'); $scope.headers['content-type'] = headers('content-type'); $scope.headers['date'] = headers('date'); $scope.headers['expires'] = headers('expires'); $scope.headers['keep-alive'] = headers('keep-alive'); $scope.headers['pragma'] = headers('pragma'); $scope.headers['server'] = headers('server'); $scope.headers['transfer-encoding'] = headers('transfer-encoding'); $scope.headers['x-powered-by'] = headers('x-powered-by'); // etc.... }); 登录,您会发现授权令牌存储在〜/ .cf / config.json下的密钥&#34; AccessToken&#34;中。你可以轻松地把它拉出来。

如果您需要自己获取授权持有者令牌而不使用<div class="row" ng-repeat="(key,value) in headers"> <div class="col-xs-4 text-right">{{ key }}</div> <div class="col-xs-8 text-left">{{ value }}</div> </div> CLI,则可以按照https://www.ng.bluemix.net/docs/#services/AppUserRegistry/index.html#appuserregistry上的说明进行操作。您也可以#!/usr/bin/perl use strict; use warnings; use autodie qw(open); use HTML::TreeBuilder::XPath; my $file = shift or die "Missing argument! Usage: $0 FILENAME\n"; open( F, $file ); my $t=HTML::TreeBuilder::XPath->new(); $t->parse_file($file) or die "Could not parse $file\n"; foreach my $img ( $t->findnodes( '//img' ) ) { my $src = $img->attr('src'); my $width = $img->attr('width'); my $height = $img->attr('height'); print $img->as_HTML, "\n"; foreach my $attr ( qw(src width height alt title) ) { print "$attr = ", $img->attr($attr), "\n" if defined($img->attr($attr)); } print "\n"; } 然后自己做cf并观看CLI与服务器进行的REST舞会以获取授权令牌。

答案 3 :(得分:0)

用户名、密码应为 base 64 编码。 Url 应该是您的 pcf 服务的登录端点。

curl https://login.<url>.com/oauth/token -X POST --user "cf:"  -H 'Content-Type: application/x-www-form-urlencoded' -d "username=<user>&password=<password>&client_id=cf&grant_type=password&response_type=token"