如何解决Coldfusion - 使用 Oauth2.cfc 获取“元素 STATUS_CODE 在作为表达式的一部分引用的 CFML 结构中未定义”
我是 oauth2 的新手,正在尝试按照此处的说明进行操作:https://github.com/coldfumonkeh/oauth2
在我的 bspace-callback.cfm 页面上请求访问令牌时出现以下错误:
Element STATUS_CODE is undefined in a CFML structure referenced as part of an expression.
The error occurred in C:/...oauth2.cfc: line 80
78 : }
79 : var result = httpService.send().getPrefix();
80 : if( '200' == result.ResponseHeader[ 'Status_Code' ] ) {
81 : stuResponse.success = true;
82 : stuResponse.content = result.FileContent;
这是我的页面:
bspace.cfm
<cfset myCfcObject = createObject("component","brightspace")>
<cfset oOauth2 = myCfcObject.init(
client_id = 'xxx',client_secret = 'xxx',redirect_uri = 'https://.../bspace-callback.cfm')>
<cfdump var="#myCfcObject#" label="myCfcObject's information."><BR>
<cfset strURL = oOauth2.buildredirectToAuthURL('enrollment:orgunit:create group:resource:permissions')>
<cfoutput>
<p>
<a href="#strURL#">LOGIN!</a>
</p>
</cfoutput>
bspace-callback.cfm
<cfsetting requestTimeOut = "9000" />
<cfset myCfcObject = createObject("component","brightspace")>
<cfset oOauth2 = myCfcObject.init(
client_id = 'xxx',redirect_uri = 'https://.../bspace-callback.cfm')>
<cfdump var="#myCfcObject#" label="myCfcObject's information."><BR>
<cfif isdefined('url.code')>
<cfset returnData = oOauth2.makeAccesstokenRequest( code = url.code )>
<cfdump var="#returnData#" label="the access token information">
</cfif>
brightspace.cfc(由我创建)
component extends="oauth2" accessors="true" {
property name="client_id" type="string";
property name="client_secret" type="string";
property name="authEndpoint" type="string";
property name="accesstokenEndpoint" type="string";
property name="redirect_uri" type="string";
/**
* I return an initialized Brightspace object instance.
* @client_id The client ID for your application.
* @client_secret The client secret for your application.
* @authEndpoint The URL endpoint that handles the authorisation.
* @accesstokenEndpoint The URL endpoint that handles retrieving the access token.
* @redirect_uri The URL to redirect the user back to following authentication.
**/
public brightspace function init(
required string client_id,required string client_secret,required string authEndpoint = 'https://auth.brightspace.com/oauth2/auth',required string accesstokenEndpoint = 'https://auth.brightspace.com/core/connect/token',required string redirect_uri
)
{
super.init(
client_id = arguments.client_id,client_secret = arguments.client_secret,authEndpoint = arguments.authEndpoint,accesstokenEndpoint = arguments.accesstokenEndpoint,redirect_uri = arguments.redirect_uri
);
return this;
}
/**
* I return the URL as a string which we use to redirect the user for authentication.
**/
public string function buildredirectToAuthURL(
required string scope
){
var sParams = {
'response_type' = 'code','scope' = arguments.scope
};
return super.buildredirectToAuthURL( sParams );
}
/**
* I make the HTTP request to obtain the access token.
* @code The code returned from the authentication request.
**/
public struct function makeAccesstokenRequest(
required string code
){
var aFormFields = [
{
'name' = 'grant_type','value' = 'authorization_code'
}
];
return super.makeAccesstokenRequest(
code = arguments.code,formfields = aFormFields
);
}
}
oauth2.cfc(来自上面的 github 网站)
/**
* @displayname oauth2
* @output false
* @hint The oauth2 object.
* @author Matt Gifford
* @website https://www.monkehworks.com
* @purpose A ColdFusion Component to manage authentication using the OAuth2 protocol.
**/
component accessors="true"{
property name="client_id" type="string";
property name="client_secret" type="string";
property name="authEndpoint" type="string";
property name="accesstokenEndpoint" type="string";
property name="redirect_uri" type="string";
/**
* I return an initialized oauth2 object instance.
* @client_id The client ID for your application.
* @client_secret The client secret for your application.
* @authEndpoint The URL endpoint that handles the authorisation.
* @accesstokenEndpoint The URL endpoint that handles retrieving the access token.
* @redirect_uri The URL to redirect the user back to following authentication.
**/
public oauth2 function init(
required string client_id,required string authEndpoint,required string accesstokenEndpoint,required string redirect_uri
){
setClient_id( arguments.client_id );
setClient_secret( arguments.client_secret );
setAuthEndpoint( arguments.authEndpoint );
setAccesstokenEndpoint( arguments.accesstokenEndpoint );
setRedirect_uri( arguments.redirect_uri );
return this;
}
/**
* I return the URL as a string which we use to redirect the user for authentication. |
* The method will handle the client_id and redirect_uri values for you. Anything else you need to send to the provider in the URL can be sent via the parameters argument.
* @parameters A structure containing key / value pairs of data to be included in the URL string.
**/
public string function buildredirectToAuthURL( struct parameters={} ) {
return getAuthEndpoint() & '?client_id=' & getClient_id() & '&redirect_uri=' & getRedirect_uri() & buildParamString( argScope = arguments.parameters );
}
/**
* I make the HTTP request to obtain the access token.
* @code The code returned from the authentication request.
* @formfields An optional array of structs for the provider requirements to add new form fields.
* @headers An optional array of structs to add custom headers to the request if required.
**/
public struct function makeAccesstokenRequest(
required string code,array formfields = [],array headers = []
){
var stuResponse = {};
var httpService = new http();
httpService.setMethod( "post" );
httpService.setCharset( "utf-8" );
httpService.setUrl( getAccesstokenEndpoint() );
if( arrayLen( arguments.headers ) ){
for( var item in arguments.headers ){
httpService.addParam( type="header",name=item[ 'name' ],value=item[ 'value' ] );
}
}
httpService.addParam( type="formfield",name="client_id",value=getClient_id() );
httpService.addParam( type="formfield",name="client_secret",value=getClient_secret() );
httpService.addParam( type="formfield",name="code",value=arguments.code );
httpService.addParam( type="formfield",name="redirect_uri",value=getRedirect_uri() );
if( arrayLen( arguments.formfields ) ){
for( var item in arguments.formfields ){
httpService.addParam( type="formfield",value=item[ 'value' ] );
}
}
var result = httpService.send().getPrefix();
if( '200' == result.ResponseHeader[ 'Status_Code' ] ) {
stuResponse.success = true;
stuResponse.content = result.FileContent;
} else {
stuResponse.success = false;
stuResponse.content = result.Statuscode;
}
return stuResponse;
}
/**
* I make the HTTP request to refresh the access token.
* @refresh_token The refresh_token returned from the accesstokenRequest request.
**/
public struct function refreshAccesstokenRequest(
required string refresh_token,array headers = []
){
var stuResponse = {};
var httpService = new http();
httpService.setMethod( "post" );
httpService.setCharset( "utf-8" );
httpService.setUrl( getAccesstokenEndpoint() );
httpService.addParam( type="header",name="Content-Type",value="application/x-www-form-urlencoded" );
if( arrayLen( arguments.headers ) ){
for( var item in arguments.headers ){
httpService.addParam( type="header",name="refresh_token",value=arguments.refresh_token );
httpService.addParam( type="formfield",name="grant_type",value="refresh_token" );
if( arrayLen( arguments.formfields ) ){
for( var item in arguments.formfields ){
httpService.addParam( type="formfield",value=item[ 'value' ] );
}
}
var result = httpService.send().getPrefix();
if( '200' == result.ResponseHeader[ 'Status_Code' ] ) {
stuResponse.success = true;
stuResponse.content = result.FileContent;
} else {
stuResponse.success = false;
stuResponse.content = result.Statuscode;
}
return stuResponse;
}
/**
* I return a string containing any extra URL parameters to concatenate and pass through when authenticating.
* @argScope A structure containing key / value pairs of data to be included in the URL string.
**/
public string function buildParamString( struct argScope={} ) {
var strURLParam = '';
if( structCount( arguments.argScope ) ) {
for( var key in arguments.argScope ) {
if( listLen( strURLParam ) ) {
strURLParam = strURLParam & '&';
}
strURLParam = strURLParam & lcase( key ) & '=' & trim( arguments.argScope[ key ] );
}
strURLParam = '&' & strURLParam;
}
return strURLParam;
}
/**
* Returns the properties as a struct
*/
public struct function getMemento(){
var result = {};
for( var thisProp in getMetaData( this ).properties ){
if( structKeyExists( variables,thisProp[ 'name' ] ) ){
result[ thisProp[ 'name' ] ] = variables[ thisProp[ 'name' ] ];
}
}
return result;
}
}
我不确定如何解决,因为当我用谷歌搜索时似乎没有其他人遇到这个问题,所以我知道我一定做错了什么。
非常感谢您的帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。