Skip to main content
Oct 28, 2011 4:16 PM UTC
Question

How can i get the HTML code of a page loaded in StageWebView???

Hi everybody, I need your help please. I develope a Flex Mobile App to connect Twitter API. The app needs to display a PIN from Twitter in a webpage, the page loaded in a StageWebView instance. The PIN is in the HTML code, I dont found any property or method to get the HTML of the content loaded and parse this string to find get PIN. Somebody know how can i do ti? Thanks.

http://www.movieclip.com.mx/Imagenes/Forums/twitter.jpg

This topic is closed to new replies. Start a new post to keep the conversation going.

5 replies

Participant
Mar 27, 2013 7:07 AM UTC

Hi,

Pinless integration of Twitter API.

Below code doesn't ask the user to enter PIN.

You will have to add "as3crypto.swc"  & "oauth-as3.swc" files in your project.

Thanks & Enjoy

Prakash Bellara

http://prakashbellara.blogspot.in/



<?xml version="1.0" encoding="utf-8"?>

<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"

    xmlns:s="library://ns.adobe.com/flex/spark"

    title="HomeView"

    creationComplete="onCreationComplete(event)">

  <fx:Script>

    <![CDATA[

      import mx.collections.ArrayCollection;

      import mx.collections.SortField;

      import mx.events.FlexEvent;

      import org.iotashan.oauth.OAuthConsumer;

      import org.iotashan.oauth.OAuthRequest;

      import org.iotashan.oauth.OAuthSignatureMethod_HMAC_SHA1;

      import org.iotashan.oauth.OAuthToken;

      import org.iotashan.utils.OAuthUtil;

      import org.iotashan.utils.URLEncoding;

      import spark.collections.Sort;

      // App Constants

      public const T_CONSUMER_KEY:String = "YOUR_CONSUMER_KEY";

      public const T_CONSUMER_SECRET:String = "YOUR_CONSUMER_SECRET" ;

      private var signature:OAuthSignatureMethod_HMAC_SHA1 = new OAuthSignatureMethod_HMAC_SHA1();

      private var TWITTER_API_URL:String = "https://api.twitter.com";


      public const VERIFY_CREDENTIALS:String   = TWITTER_API_URL + "/1.1/account/verify_credentials.json";

      private var twitterRequestURL:String   = TWITTER_API_URL + "/oauth/request_token";

      private var twitterAuthURL:String     = TWITTER_API_URL + "/oauth/authorize";

      private var twitterTokenURL:String     = TWITTER_API_URL + "/oauth/access_token";

      public var twitterAccessObj:Object = {};

      private var requestToken:OAuthToken;

      private var accessToken:OAuthToken;

      private var oAuthConsumer:OAuthConsumer;

      private var twitterWebView:StageWebView;

      private var webViewStartLocation:int;

      private var accessRequest:OAuthRequest;

      private var thisProfile:Object = {};

      private var twitterUsrDtls:Object;

      [Bindable]

      var twitterfollowerList:ArrayCollection ;


      // Creation Complete

      private function onCreationComplete(event:FlexEvent):void

      {

        oAuthConsumer = new OAuthConsumer(T_CONSUMER_KEY, T_CONSUMER_SECRET);

        var oauth:OAuthRequest = new OAuthRequest("GET", twitterRequestURL, null, oAuthConsumer);

        var request:URLRequest = new URLRequest(oauth.buildRequest(signature));

        var loader:URLLoader = new URLLoader(request);

        loader.addEventListener(Event.COMPLETE,onLoaderComplete);

      }

      //Load request URL for Twitter Access Token

      private function onLoaderComplete(e:Event):void

      {

        requestToken = OAuthUtil.getTokenFromResponse(e.currentTarget.data);

        var authRequest:URLRequest = new URLRequest('http://api.twitter.com/oauth/authorize?oauth_token=' + requestToken.key);

        // StageWebView to Authorize the App

        twitterWebView = new StageWebView();

        twitterWebView.viewPort = new Rectangle(10,10,(stage.width-40),stage.height-40);

        twitterWebView.stage = this.stage;

        twitterWebView.assignFocus();

        twitterWebView.loadURL(authRequest.url);

        twitterWebView.addEventListener(LocationChangeEvent.LOCATION_CHANGE, onLocationChange);

      }

      // Location has Changed check for call back url

      private function onLocationChange(e:LocationChangeEvent):void

      {

        var location:String = e.location;

        if(location.search("oauth_verifier") != -1)

        {

          busyInd.visible = true;

          var oAuthVerifier:String = location.substr(location.search("oauth_verifier") + 15);

          validatePin(oAuthVerifier);

        }

      }

      private function validatePin(oAuthVerifier:String):void

      {

        var params:Object = new Object();

        params.oauth_verifier = oAuthVerifier;

        accessRequest = new OAuthRequest("GET", twitterTokenURL, params, oAuthConsumer, requestToken);

        var accessUrlRequest:URLRequest = new URLRequest(accessRequest.buildRequest(signature));

        var accessLoader:URLLoader = new URLLoader(accessUrlRequest);

        accessLoader.addEventListener(Event.COMPLETE, onAccessRequestComplete);

      }

      // We are now ready we have now got access token & secert from the twitter server.

      private function onAccessRequestComplete(e:Event):void{

        accessToken = OAuthUtil.getTokenFromResponse(e.currentTarget.data);

        twitterAccessObj.accessKey = accessToken.key;

        twitterAccessObj.accessSecret = accessToken.secret;

        twitterWebView.dispose();

        getMyFollowersList();

      }

      private function getMyFollowersList():void

      {

        var params:Object = {};

        params.status = "";

        var consumer:OAuthConsumer = new OAuthConsumer(T_CONSUMER_KEY, T_CONSUMER_SECRET);

        var token:OAuthToken = new OAuthToken(twitterAccessObj.accessKey, twitterAccessObj.accessSecret);

        var postRequest:OAuthRequest = new OAuthRequest("GET",TWITTER_API_URL+'/1/statuses/friends.json',null,consumer, token);

        var urlRequest:URLRequest = new URLRequest(postRequest.buildRequest(signature));

        var loader:URLLoader = new URLLoader(urlRequest);

        loader.addEventListener(Event.COMPLETE, onTwitterFollowerComplete);

        loader.addEventListener(IOErrorEvent.IO_ERROR, onTwitterIOError);

        loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onTwitterHttpStatus);

      }

      private function onTwitterFollowerComplete(e:Event):void

      {

        followerConainer.visible = followerConainer.includeInLayout = true;

        listTwitter.visible = listTwitter.includeInLayout = true;

        trace("Got mmy first 100 Followers list Success!");

        twitterfollowerList = new ArrayCollection();

        var tmpArrCol:ArrayCollection = new ArrayCollection();

        var obj:Object = JSON.parse(e.currentTarget.data.toString());

        for each(var i: Object in obj){

          tmpArrCol.addItem(i);

        }

        var dataSortField:SortField = new SortField();

        dataSortField.name = "name";

        var numericDataSort:spark.collections.Sort = new Sort();

        numericDataSort.fields = [dataSortField];

        tmpArrCol.sort = numericDataSort;

        tmpArrCol.refresh();

        twitterfollowerList = tmpArrCol;

        twitterfollowerList.refresh();

        busyInd.visible = false;

      }

      private function onTwitterIOError(e:IOErrorEvent):void

      {

        busyInd.visible = false;

        trace("IOError!");

      }

      private function onTwitterHttpStatus(e:HTTPStatusEvent):void

      {

        trace("HttpStatus!");

      }

    ]]>

  </fx:Script>

  <s:SkinnableContainer width="100%" height="100%"

              top="10" bottom="10" left="10" right="10"

              backgroundAlpha="0">

    <s:layout>

      <s:VerticalLayout horizontalAlign="center"  />

    </s:layout>

    <s:VGroup id="followerConainer"

          includeInLayout="false"

          visible="false"

          horizontalAlign="center"

          width="100%" height="100%">

      <s:List id="listTwitter"

          width="100%" color="blue" height="100%"

          labelField="name"  dataProvider="{twitterfollowerList}" visible="false"> 

        <s:layout>

          <s:VerticalLayout horizontalAlign="justify" gap="0" requestedRowCount="6"  verticalAlign="middle"/>

        </s:layout>

        <s:itemRenderer>

          <fx:Component>

            <s:IconItemRenderer creationComplete="iconitemrenderer1_creationCompleteHandler(event)"

                      iconField="profile_image_url"  alternatingItemColors="#E1E1E1" messageField="text" labelField="name"  top="0" bottom="0"  verticalAlign="middle">

              <fx:Script>

                <![CDATA[

                  import mx.events.FlexEvent;

                  protected function iconitemrenderer1_creationCompleteHandler(event:FlexEvent):void

                  {

                    iconWidth=40;

                    iconHeight=40;

                    height=50;

                  }

                ]]>

              </fx:Script>

            </s:IconItemRenderer>

          </fx:Component>

        </s:itemRenderer>

      </s:List>     

    </s:VGroup>

  </s:SkinnableContainer>

  <s:actionContent>

    <s:BusyIndicator id="busyInd" visible="false" />

  </s:actionContent>

</s:View>

Translate
Nov 2, 2011 5:43 PM UTC

Thanks everybody for your tips. Well this is my solution....

browser_swv.addEventListener(flash.events.Event.COMPLETE,onPageLoaded);

browser_swv.loadURL("http://www.twitter.com");

protected function onPageLoaded(ev:Event):void{

     browser_swv.loadURL('javascript:document.title=document.documentElement.innerHTML;');

     var html_str:String = browser_swv.title;

    

     /* HERE THE CODE TO FIND SOME TAG IN THE HTML */

}

Translate
Oct 29, 2011 10:41 PM UTC

Would http://code.google.com/p/stagewebviewbridge/ do what you need?

It allows communication between JS / AS through the StageWebView.

Translate
Participant
Oct 29, 2011 11:28 PM UTC

Thank for your comment, I tried the StageWebViewBridge but the only way to add a JS in the twitter page is to use iFrames, the problem is Twiiter dettects iFrames and the page dont load. Any other suggest?

Translate
Oct 30, 2011 8:06 AM UTC

Not sure... but when the twitter page is loaded completely you can try loading a javascript:/*some code*/ where the code reads the PIN from DOM and sets PIN as the title of document and then you can read the title of page in Actionscript.

Translate