Páginas filhas
  • FWFacebook

Exemplo de uso da Classe FWoAuth2Client com Client para Facebook.


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



User Function fb()
Local oAuth, cResult, oBj

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

    I18nConOut("#1#1#2#1", {CRLF, "Waiting for response..."})

    oAuth:SetScopes({"publish_actions"})
//  oAuth:SetScopes({"user_friends"})

//  oAuth:SetUniqueToken("CAAGHtCBf4fEBAFIUwX46WkUQ7ZC....")

//  cResult := oAuth:Access("https://graph.facebook.com/me/", "fields=id,name,picture", , .T.)
//  cResult := oAuth:Access("https://graph.facebook.com/me/feed")
//  cResult := oAuth:Access("https://graph.facebook.com/me/permissions")
    cResult := oAuth:Access("https://graph.facebook.com/me/feed", , "message=Hello, World. By Framework Protheus", .F.)

    I18nConOut("#1Result: #2 #1", {CRLF, cResult})

    oBj := Nil
    If FWJsonDeserialize(cResult, @oBj)
        cResult := oAuth:Access(i18n("https://graph.facebook.com/#1", {oBj:id}), "method=delete", , .F.)
    EndIf

    I18nConOut("#1Result: #2 #1", {CRLF, cResult})

    I18nConOut("#1Token used: #2 #1", {CRLF, oAuth:GetToken()})
    
    oAuth:Destroy()

Return 


Class FWFaceBook From FWoAuth2Client

    Data aScopes

    Method New() CONSTRUCTOR
    Method Destroy()
    Method ClassName()
    Method SetScopes()
    Method Access()
    Method Debug_Token()
    Method Valid_Token()

EndClass

Method New(cConsumer,cSecret) Class FWFaceBook
Local oURL      := FwoAuth2Url():New("https://www.facebook.com/dialog/oauth", "https://graph.facebook.com/oauth/access_token")

    _Super:New(cConsumer,cSecret,oURL)

    ::SetScopes({"basic_info"})
    ::SetAsCode()
    ::SetQueryAuthorization()
    
Return Self

Method Destroy() Class FWFaceBook
    ::aScopes := aSize(::aScopes, 0)
Return _Super:Destroy()

Method ClassName() Class FWFaceBook
Return "FWFaceBook"

Method SetScopes(aScopes) Class FWFaceBook
Local cScope
Local nI

PARAMTYPE 0 VAR aScopes AS ARRAY OPTIONAL Default {}

    ::aScopes := aScopes

    For nI := 1 to Len(aScopes)
        If nI == 1
            cScope := "scope="+aScopes[nI]
        Else
            cScope += "," + aScopes[nI]
        EndIf
    Next 

    ::SetAuthOptions(cScope)

Return Len(aScopes) > 0

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

Default lGet := .T.

    If !::Valid_Token()
        ::ClearToken()
    EndIf

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

Return cResult

Method Debug_Token(cToken) Class FWFaceBook
Local cResult
Local aHeaderRet
Local oDebug

Default cToken := ::GetToken()

    If !Empty(cToken)

        cResult := ::Get(I18n("https://graph.facebook.com/debug_token?input_token=#1&access_token=#1", {cToken}), , , @aHeaderRet)

        If aScan(aHeaderRet, {|x| At("content-type:", Lower(x)) > 0 .And. At("application/json", Lower(x)) > 0 }) > 0
            FWJsonDeserialize(cResult, @oDebug)
        EndIf

    EndIf

Return oDebug

Method Valid_Token() Class FWFaceBook
Local lRet   := .F.
Local cToken := ::GetToken()
Local oDebug
Local nI
Local aProps

    If !Empty(cToken)

        oDebug := ::Debug_Token(cToken)
        If oDebug != Nil

            aProps := ClassDataArr(oDebug)
            If aScan(aProps, {|x| lower(x[1]) == 'data' }) > 0

                If oDebug:data:is_valid

                    lRet := .T.
                    For nI := 1 to Len(::aScopes)
                        If !aScan(oDebug:data:scopes, {|x| x == ::aScopes[nI]}) > 0
                            lRet := .F.
                            Exit
                        EndIf
                    Next

                EndIf

            EndIf

        EndIf

    EndIf

Return lRet