// (c) 2018, Questetra, Inc. (the MIT License)
//// == OAuth2 Setting example ==
// Authorization Endpoint URL:
// "https://public-api.wordpress.com/oauth2/authorize"
// Token Endpoint URL:
// "https://public-api.wordpress.com/oauth2/token"
// Scope: "global" (or "auth")
// Client ID: ( from https://developer.wordpress.com/apps/ )
// Consumer Secret: ( from https://developer.wordpress.com/apps/ )
// -- "Redirect URLs": "https://s.questetra.net/oauth2callback"
// -more-
// https://developer.wordpress.com/docs/oauth2/
// https://developer.wordpress.com/docs/api/1.1/get/sites/%24site/stats/post/%24post_id/
//////// START "main()" ////////
main();
function main(){
//// == Config Retrieving / 工程コンフィグの参照 ==
const oauth2 = configs.get( "conf_OAuth2" ) + "";
const wpDomain = configs.get( "conf_WpDomain" ) + "";
const postId = configs.get( "conf_PostId" ) + "";
const dataIdD = configs.get( "conf_DataIdD" ) + "";
// 'java.lang.String' (String Obj) to javascript primitive 'string'
if(postId === ""){
throw new Error( '\n PostID not Defined \n' );
}
if(wpDomain === ""){
throw new Error( '\n WordPress Domain not Defined \n' );
}
//// == Data Retrieving / ワークフローデータの参照 ==
// (nothing)
//// == Calculating / 演算 ==
/// obtain OAuth2 Access Token
const token = httpClient.getOAuth2Token( oauth2 );
/// prepare API Request
let apiRequest = httpClient.begin(); // HttpRequestWrapper
// - com.questetra.bpms.core.event.scripttask.HttpClientWrapper
apiRequest = apiRequest.bearer( token );
apiRequest = apiRequest.queryParam( "fields", "views" );
/// access API (with post(), get(), put(), etc.)
const apiUri = "https://public-api.wordpress.com/rest/v1.1/sites/"
+ wpDomain + "/stats/post/" + postId;
const response = apiRequest.get( apiUri );
// - HttpResponseWrapper
const httpStatus = response.getStatusCode() + "";
engine.log( "STATUS: " + httpStatus );
const responseStr = response.getResponseAsString() + "";
engine.log( "RESPONSE: \n" + responseStr );
/// parse Response-JSON
const responseObj = JSON.parse( responseStr );
//// == Data Updating / ワークフローデータへの代入 ==
if( engine.findDataDefinitionByNumber( dataIdD ).matchDataType( "DECIMAL" ) ){
engine.setDataByNumber( dataIdD, new java.math.BigDecimal(responseObj.views) );
}else{
engine.setDataByNumber( dataIdD, responseObj.views );
}
} //////// END "main()" ////////
ピンバック: SaaS WordPress 体験記 - Questetra
ピンバック: My Experience Story on SaaS WordPress - Questetra