在没有 OAuth 的情况下使用 Google API 灵活访问 public 日历
Flex to access a public calendar using Google API without OAuth
我正在尝试使用 Google API 访问 public 假期日历。我从一些问答 Whosebug 中了解到,访问 public 日历不需要授权,并且使用 API 密钥我可以检索特定国家/地区的所有假期。下面是我在网络浏览器中输入的 URL 并且能够在没有 OAuth 的情况下转储所有假期。 https://www.googleapis.com/calendar/v3/calendars/en.canadian%23holiday%40group.v.calendar.google.com/events?key=myAPIkey
将 myAPIkey 替换为 google 生成的 API key 将给我在浏览器中倾倒的所有加拿大假期。
所以,我编写了下面的 Flex 程序,但返回的 evt.result 不包含任何数据。我尝试了不同的 calendarService.resultFormat 但 none 仍然有效。请帮忙。
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="doInit()">
<fx:Script>
<![CDATA[
import mx.rpc.Responder;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
private var calendarService:HTTPService;
private var success:Boolean = true;
protected function doInit():void
{
calendarService = new HTTPService();
calendarService.method = "GET";
//calendarService.resultFormat = HTTPService.RESULT_FORMAT_ARRAY;
//calendarService.resultFormat = HTTPService.RESULT_FORMAT_E4X;
calendarService.resultFormat = HTTPService.RESULT_FORMAT_FLASHVARS;
//calendarService.resultFormat = HTTPService.RESULT_FORMAT_OBJECT;
//calendarService.resultFormat = HTTPService.RESULT_FORMAT_TEXT;
//calendarService.resultFormat = HTTPService.RESULT_FORMAT_XML;
}
public function getCalendarByCountry():void
{
calendarService.url = "https://www.googleapis.com/calendar/v3/calendars/en.canadian%23holiday%40group.v.calendar.google.com/events?key=<myAPIkey>";
respCalendar.token = calendarService.send();
respCalendar.addEventListener(ResultEvent.RESULT, onCalendarsResponse);
respCalendar.addEventListener(FaultEvent.FAULT, onCalendarsFault);
}
private function onCalendarsResponse(evt:ResultEvent):void
{
respCalendar.removeEventListener(ResultEvent.RESULT, onCalendarsResponse);
success = true;
}
private function onCalendarsFault(evt:FaultEvent):void
{
respCalendar.removeEventListener(ResultEvent.RESULT, onCalendarsFault);
success = false;
}
]]>
</fx:Script>
<fx:Declarations>
<mx:CallResponder id="respCalendar"/>
</fx:Declarations>
<mx:Button label="Get" click="getCalendarByCountry()"/>
</mx:Application>
将 resultFormat 更改为 HTTPService.RESULT_FORMAT_TEXT 并在 evt.result.
上使用 JSON.parse()
有关 JSON.parse() 的更多信息,请点击此处:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/JSON.html
我正在尝试使用 Google API 访问 public 假期日历。我从一些问答 Whosebug 中了解到,访问 public 日历不需要授权,并且使用 API 密钥我可以检索特定国家/地区的所有假期。下面是我在网络浏览器中输入的 URL 并且能够在没有 OAuth 的情况下转储所有假期。 https://www.googleapis.com/calendar/v3/calendars/en.canadian%23holiday%40group.v.calendar.google.com/events?key=myAPIkey 将 myAPIkey 替换为 google 生成的 API key 将给我在浏览器中倾倒的所有加拿大假期。 所以,我编写了下面的 Flex 程序,但返回的 evt.result 不包含任何数据。我尝试了不同的 calendarService.resultFormat 但 none 仍然有效。请帮忙。
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="doInit()">
<fx:Script>
<![CDATA[
import mx.rpc.Responder;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
private var calendarService:HTTPService;
private var success:Boolean = true;
protected function doInit():void
{
calendarService = new HTTPService();
calendarService.method = "GET";
//calendarService.resultFormat = HTTPService.RESULT_FORMAT_ARRAY;
//calendarService.resultFormat = HTTPService.RESULT_FORMAT_E4X;
calendarService.resultFormat = HTTPService.RESULT_FORMAT_FLASHVARS;
//calendarService.resultFormat = HTTPService.RESULT_FORMAT_OBJECT;
//calendarService.resultFormat = HTTPService.RESULT_FORMAT_TEXT;
//calendarService.resultFormat = HTTPService.RESULT_FORMAT_XML;
}
public function getCalendarByCountry():void
{
calendarService.url = "https://www.googleapis.com/calendar/v3/calendars/en.canadian%23holiday%40group.v.calendar.google.com/events?key=<myAPIkey>";
respCalendar.token = calendarService.send();
respCalendar.addEventListener(ResultEvent.RESULT, onCalendarsResponse);
respCalendar.addEventListener(FaultEvent.FAULT, onCalendarsFault);
}
private function onCalendarsResponse(evt:ResultEvent):void
{
respCalendar.removeEventListener(ResultEvent.RESULT, onCalendarsResponse);
success = true;
}
private function onCalendarsFault(evt:FaultEvent):void
{
respCalendar.removeEventListener(ResultEvent.RESULT, onCalendarsFault);
success = false;
}
]]>
</fx:Script>
<fx:Declarations>
<mx:CallResponder id="respCalendar"/>
</fx:Declarations>
<mx:Button label="Get" click="getCalendarByCountry()"/>
</mx:Application>
将 resultFormat 更改为 HTTPService.RESULT_FORMAT_TEXT 并在 evt.result.
上使用 JSON.parse()有关 JSON.parse() 的更多信息,请点击此处:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/JSON.html