Páginas filhas
  • FWTwitter

Exemplo de uso da Classe FWoAuth2Client com Client para Twitter.


#Include 'Protheus.ch'
#Include 'Parmtype.ch'


User Function Twitter()
Local oAuth, cResult
Local cUrl := "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi"


    oAuth := FWTwitter():New("", "") // App Key e App Secret

    cResult := oAuth:Access(cURL, , , .T.)
    I18nConOut("#1Result: #2 #1", {CRLF, cResult})

Return 

Class FWTwitter From FWoAuth2Client


    Method New() CONSTRUCTOR
    Method ClassName()
    Method GetToken()
    Method Access()

EndClass

Method New(cConsumer,cSecret) Class FWTwitter
Local oURL := FWoAuth2URL():New("","https://api.twitter.com/oauth2/token")

    _Super:New(cConsumer,cSecret,oURL)

    ::SetAsImplicit()
    ::SetHeaderAuthorization("Authorization")

Return Self

Method ClassName() Class FWTwitter
Return "FWTwitter"

Method GetToken() Class FWTwitter
Local aHeadOut := {}
Local cHeadRet := ""
Local oJS      := Nil
Local uValue   := Nil

    If Empty(::cToken)

        aAdd(aHeadOut, I18n("#1 #2", {"Authorization: Basic ", Encode64(I18n("#1:#2", {::cConsumerKey, ::cSecret}))}))
        cResult := HTTPPost(::oURL:cToken, , "grant_type=client_credentials",, aHeadOut, @cHeadRet)
//      I18nConOut("#1Result: #2 #1", {CRLF, cResult})

        oJS := FwJsonObject():New()
        If oJS:Activate(cResult)
            If oJS:GetValueStr(@uValue,"access_token")
                ::SetUniqueToken("Bearer "+uValue)
            EndIf
        EndIf

        aHeadOut := aSize(aHeadOut, 0)
        oJS:Deactivate()
        FreeObj(oJS)

    EndIf

Return ::cToken

Method Access(cURLResource, cQuery, cBody, lGet, aHeader) Class FWTwitter
Local cResult := ""

Default lGet := .T.

    ::GetToken() // Busco pelo token

    cResult := _Super:Access(cURLResource, cQuery, cBody, lGet, @aHeader)

Return cResult