GET https ? (Box.net API)

Für allgemeine Fragen zur Programmierung mit PureBasic.
Benutzeravatar
Thorsten1867
Beiträge: 1360
Registriert: 04.02.2005 15:40
Computerausstattung: [Windows 10 x64] [PB V5.7x]
Wohnort: Kaufbeuren
Kontaktdaten:

GET https ? (Box.net API)

Beitrag von Thorsten1867 »

Um auf die API von Box.net zugreifen zu können, benötige ich z.B. folgende Abfrage "GET https://www.box.com/api/1.0/rest?action ... _key={your api key}".

Meine bisherigen Versuche mit ReceiveHTTPFile(URL$, Dateiname$) waren nicht erfolgreich.

Hat jemand eine Idee, wie sich ein Zugriff auf auf die Box.net API realisieren ließe?

( http://developers.box.com/get-started/ )
Zuletzt geändert von Thorsten1867 am 17.11.2012 17:49, insgesamt 1-mal geändert.
Download of PureBasic - Module
Download of PureBasic - Programmes

[Windows 11 x64] [PB V6]

Bild
Benutzeravatar
Bisonte
Beiträge: 2476
Registriert: 01.04.2007 20:18

Re: GET https ? (Box.net API)

Beitrag von Bisonte »

da kommt dann nur HTTP(s) GET/POST in frage.... will sagen, muss von Hand mit den Network Befehlen passieren.
PureBasic 6.21 (Windows x86/x64) | Windows11 Pro x64 | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | GeForce RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
Benutzeravatar
kunstlust
Beiträge: 259
Registriert: 12.04.2012 23:47
Wohnort: Hannover
Kontaktdaten:

Re: GET https ? (Box.net API)

Beitrag von kunstlust »

Benutzeravatar
Thorsten1867
Beiträge: 1360
Registriert: 04.02.2005 15:40
Computerausstattung: [Windows 10 x64] [PB V5.7x]
Wohnort: Kaufbeuren
Kontaktdaten:

AW: GET https ? (Box.net API)

Beitrag von Thorsten1867 »

Danke, ich werde das mal ausprobieren.
Download of PureBasic - Module
Download of PureBasic - Programmes

[Windows 11 x64] [PB V6]

Bild
Benutzeravatar
Morty
Beiträge: 268
Registriert: 22.10.2004 07:20
Wohnort: Erfurt
Kontaktdaten:

Re: GET https ? (Box.net API)

Beitrag von Morty »

Hi.

Ich hab schonmal damit begonnen. Wenn Du bis Mittwoch warten kannst, würde ich mal raus suchen, was ich damals gemacht hab. War nicht viel, aber der Grundsatz hat funktioniert.

Gruß, Morty
Benutzeravatar
Thorsten1867
Beiträge: 1360
Registriert: 04.02.2005 15:40
Computerausstattung: [Windows 10 x64] [PB V5.7x]
Wohnort: Kaufbeuren
Kontaktdaten:

Re: GET https ? (Box.net API)

Beitrag von Thorsten1867 »

Klar kann ich warten. :-)
Die Cloud-Anbindung für mein Programm ist im Moment nur so eine Idee.
Erst wenn die Tests mit der API erfolgreich sind, werde ich mir weitere Gedanken machen.
Download of PureBasic - Module
Download of PureBasic - Programmes

[Windows 11 x64] [PB V6]

Bild
Benutzeravatar
Morty
Beiträge: 268
Registriert: 22.10.2004 07:20
Wohnort: Erfurt
Kontaktdaten:

Re: GET https ? (Box.net API)

Beitrag von Morty »

Mahlzeit

Wie gesagt, ich hatte damals mal damit begonnen, das Projekt aber wieder einschlafen lassen.
Es ist nur ein Anfang, mit dem ich probiert habeb. Dies kann als Ansatz dienen. Die Dokumentation der BOX.API ist eigentlich ziemlich gut,
und so kann man das hier leicht erweitern.
Es fehlt noch eine ganze Menge, aber das kann man alles via XML auslesen verarbeiten ... gerade bei get_account_tree()
Vielleicht können wir das ja gemeinsam erarbeiten :wink:

Wichtig, man benötigt einen eigenen API Key. Den muss man sich vorher registrieren lassen.

Wie bereits erwähnt, ist das lediglich ein Test:
Vorgehen: Programm starten, sich im Webgadget anmelden, und wenn alles gut lief, den Button 'Login erfolgreich' drücken -> auf den Debug Output achten!!

Code: Alles auswählen

;BOX.net Include 

InitNetwork()

;Dieser muss generiert werden
#BOX_ApiKey = ""  

Structure __my_box_net_user_structure__
  login.s                 ;/---
  email.s                 ;| Entsprechend der OpenBox Spezifikation
  access_id.s             ;|
  user_id.s               ;|
  space_amount.s          ;|
  space_used.s            ;|
  max_upload_size.s       ;\---
EndStructure

Structure __my_box_net_structure__
  MainXMLNode.i
  ChildXMLNode.i
  BOX_NET_TICKET.s
  BOX_NET_ERROR.s       ;Kommt aus dem Status
  BOX_NET_AUTH_TOKEN.s
  BOX_NET_USER.__my_box_net_user_structure__
  LogedIn.i
  ThreadWatchWebgadget.i
  WindowID.i
  WebGadgetID.i
  ButtonGadgetID.i
  WindowEvent.i
EndStructure
Global BOX_NET.__my_box_net_structure__


Procedure.s BOX_GetTicketFromXMLFile(box_xml_file.s)
  tmp_return_string.s=""
  tmp_box_xml.i=LoadXML(#PB_Any,box_xml_file.s)
  If tmp_box_xml.i
    main_node.i=MainXMLNode(tmp_box_xml.i)
    If main_node.i
      If GetXMLNodeName(main_node.i)="response"
        tmp_box_childnode.i=ChildXMLNode(main_node.i, 1)
        If tmp_box_childnode.i And GetXMLNodeName(tmp_box_childnode.i)="status"
          If GetXMLNodeText(tmp_box_childnode.i)="get_ticket_ok"
            tmp_box_childnode.i=ChildXMLNode(main_node.i, 2)
            If tmp_box_childnode.i And GetXMLNodeName(tmp_box_childnode.i)="ticket"
              tmp_return_string.s=GetXMLNodeText(tmp_box_childnode.i)
            EndIf
          Else
            BOX_NET\BOX_NET_ERROR=GetXMLNodeText(tmp_box_childnode.i)
          EndIf
        EndIf
      EndIf
    EndIf
    FreeXML(tmp_box_xml.i)
  EndIf
  
  ProcedureReturn tmp_return_string.s
EndProcedure

Procedure.s BOX_GetAuthTokenFromXMLFile(box_xml_file.s)
  tmp_return_string.s=""
  tmp_box_xml.i=LoadXML(#PB_Any,box_xml_file.s)
  If tmp_box_xml.i
    main_node.i=MainXMLNode(tmp_box_xml.i)
    If main_node.i
      If GetXMLNodeName(main_node.i)="response"
        tmp_box_childnode.i=ChildXMLNode(main_node.i, 1)
        If tmp_box_childnode.i And GetXMLNodeName(tmp_box_childnode.i)="status"
          If GetXMLNodeText(tmp_box_childnode.i)="get_auth_token_ok"
            token_childnode.i=ChildXMLNode(main_node.i, 2)    
            If token_childnode.i And GetXMLNodeName(token_childnode.i)="auth_token"
              tmp_return_string.s=GetXMLNodeText(token_childnode.i)
            EndIf
          Else
            BOX_NET\BOX_NET_ERROR=GetXMLNodeText(tmp_box_childnode.i)
          EndIf
        EndIf
      EndIf
    EndIf
    FreeXML(tmp_box_xml.i)
  EndIf
  
  ProcedureReturn tmp_return_string.s

EndProcedure

Procedure.s BOX_get_ticket()
  ;http://developers.box.net/w/page/12923936/ApiFunction_get_ticket
  tmp_return_string.s=""
  
  If ReceiveHTTPFile("https://www.box.net/api/1.0/rest?action=get_ticket&api_key="+#BOX_ApiKey,"test_xml.xml")
    Debug "Ticket erhalten"
    BOX_NET\BOX_NET_TICKET=BOX_GetTicketFromXMLFile("test_xml.xml")
    ProcedureReturn BOX_NET\BOX_NET_TICKET
  Else
    ProcedureReturn "no_connection"
  EndIf
  
EndProcedure

Procedure BOX_SetAuthToken(tmp_token.s)
  BOX_NET\BOX_NET_AUTH_TOKEN=tmp_token.s
EndProcedure

Procedure BOX_get_auth_token()
  Debug "holen token"
  If ReceiveHTTPFile("https://www.box.net/api/1.0/rest?action=get_auth_token&api_key="+#BOX_ApiKey+"&ticket="+BOX_NET\BOX_NET_TICKET,"test_xml.xml")
    BOX_NET\BOX_NET_AUTH_TOKEN=BOX_GetAuthTokenFromXMLFile("test_xml.xml")
    Debug "AuthToken -> "+BOX_NET\BOX_NET_AUTH_TOKEN
    Debug "AuthToken / Error -> "+BOX_NET\BOX_NET_ERROR
    If CreateFile(1,"my_token.txt")
      WriteStringN(1, BOX_NET\BOX_NET_AUTH_TOKEN)
      CloseFile(1)
    EndIf
    ProcedureReturn #True
  Else
    Debug "get auth token -> no_connection"
    ProcedureReturn #False
  EndIf

EndProcedure

Procedure BOX_GetAccountInfoFromXMLFile(box_xml_file.s)
  tmp_return_value.i=#False
  
  tmp_box_xml.i=LoadXML(#PB_Any,box_xml_file.s)
  If tmp_box_xml.i
    main_node.i=MainXMLNode(tmp_box_xml.i)
    If main_node.i
      If GetXMLNodeName(main_node.i)="response"
        tmp_box_statusnode.i=ChildXMLNode(main_node.i, 1)
        If tmp_box_statusnode.i And GetXMLNodeName(tmp_box_statusnode.i)="status"       
          If GetXMLNodeText(tmp_box_statusnode.i)="get_account_info_ok"
            ;Status ok -> das nächste ist der User Node
            tmp_box_usernode.i=ChildXMLNode(main_node.i, 2)
            If tmp_box_usernode.i And GetXMLNodeName(tmp_box_usernode.i)="user"
              ;Jetzt einfach alle Child Nodes durchgehen und entsprechend verarbeiten
              tmp_c_childs.i=XMLChildCount(tmp_box_usernode.i)
              If tmp_c_childs.i>0
                For c_child.i=1 To tmp_c_childs.i
                  tmp_child_node.i=ChildXMLNode(tmp_box_usernode.i, c_child.i)
                  If tmp_child_node.i
                    ;parsen
                    Select GetXMLNodeName(tmp_child_node.i)
                      Case "login"
                        BOX_NET\BOX_NET_USER\login=GetXMLNodeText(tmp_child_node.i)
                      Case "email"
                        BOX_NET\BOX_NET_USER\email=GetXMLNodeText(tmp_child_node.i)
                      Case "access_id"
                        BOX_NET\BOX_NET_USER\access_id=GetXMLNodeText(tmp_child_node.i)
                      Case "user_id"
                        BOX_NET\BOX_NET_USER\user_id=GetXMLNodeText(tmp_child_node.i)
                      Case "space_amount"
                        BOX_NET\BOX_NET_USER\space_amount=GetXMLNodeText(tmp_child_node.i)
                      Case "space_used"
                        BOX_NET\BOX_NET_USER\space_used=GetXMLNodeText(tmp_child_node.i)
                      Case "max_upload_size"
                        BOX_NET\BOX_NET_USER\space_used=GetXMLNodeText(tmp_child_node.i)
                    EndSelect
                  EndIf
                  
                Next
                tmp_return_value.i=#True
              Else
                ;Keine Childs
                tmp_return_value.i=#False
              EndIf
            Else
              ;Kein User - Node ... na dann raus
              tmp_return_value.i=#False
            EndIf
          Else
            ;Status nicht in Ordnung
            BOX_NET\BOX_NET_ERROR=GetXMLNodeText(tmp_box_statusnode.i)
            tmp_return_value.i=#False
          EndIf
          
        Else
          ;kein Status Node -> raus
          tmp_return_value.i=#False
        EndIf
      EndIf
    EndIf
    FreeXML(tmp_box_xml.i)
  EndIf

  ProcedureReturn tmp_return_value.i
EndProcedure

Procedure BOX_get_account_info()
  Debug "account Infos auslesen"

  If ReceiveHTTPFile("https://www.box.net/api/1.0/rest?action=get_account_info&api_key="+#BOX_ApiKey+"&auth_token="+BOX_NET\BOX_NET_AUTH_TOKEN,"test_xml.xml")
    If BOX_GetAccountInfoFromXMLFile("test_xml.xml")=#True
      Debug "Account war erfolgreich"
      Debug BOX_NET\BOX_NET_USER\login
      Debug BOX_NET\BOX_NET_USER\email
      Debug BOX_NET\BOX_NET_USER\access_id
      Debug BOX_NET\BOX_NET_USER\user_id
      Debug BOX_NET\BOX_NET_USER\space_amount
      Debug BOX_NET\BOX_NET_USER\space_used
      Debug BOX_NET\BOX_NET_USER\max_upload_size
      ProcedureReturn #True
    Else
      Debug "Fehler in get_account_info"
      ProcedureReturn #False
    EndIf
  Else
    Debug "get_account_info -> no_connection"
    ProcedureReturn #False
  EndIf
  
EndProcedure

Procedure BOX_get_account_tree()

  Debug "get_account_tree"  

  If ReceiveHTTPFile("https://www.box.net/api/1.0/rest?action=get_account_tree&api_key="+#BOX_ApiKey+"&auth_token="+BOX_NET\BOX_NET_AUTH_TOKEN+"&folder_id=0&params[]=onelevel&params[]=nozip","gat_xml.xml")
    Debug "account tree erhalten"
;     Hier muss die XML Datei ausgelesen werden!!

  Else
    Debug "get_account_tree -> no_connection"
    ProcedureReturn #False
  EndIf

EndProcedure

Procedure BOX_logout()

  If ReceiveHTTPFile("https://www.box.net/api/1.0/rest?action=logout&api_key="+#BOX_ApiKey+"&auth_token="+BOX_NET\BOX_NET_AUTH_TOKEN,"test_xml.xml")
    Debug "logout"
  EndIf
  
EndProcedure


Procedure BOX_OpenLoginWin()

  BOX_NET\LogedIn=#False  

  If BOX_NET\BOX_NET_TICKET="":ProcedureReturn #False:EndIf
  
  BOX_NET\WindowID=OpenWindow(#PB_Any,#PB_Ignore, #PB_Ignore, 600, 400, "Login to BOX.NET",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
  BOX_NET\WebGadgetID=WebGadget(#PB_Any,0,0,600,370,"https://www.box.net/api/1.0/auth/"+BOX_NET\BOX_NET_TICKET)


  myBrowser.IWebBrowser2 = GetWindowLong_(GadgetID(BOX_NET\WebGadgetID), #GWL_USERDATA) 
  myBrowser\put_Silent(#True) 

  BOX_NET\ButtonGadgetID=ButtonGadget(#PB_Any,490,375,100,20,"Login erfolgreich")
  
  
  quit=0
  Repeat
    BOX_NET\WindowEvent=WaitWindowEvent()
    If BOX_NET\WindowEvent
      Select BOX_NET\WindowEvent
        Case #PB_Event_CloseWindow
          If EventWindow()=BOX_NET\WindowID:quit=1:EndIf
        Case #PB_Event_Gadget
          If EventGadget()=BOX_NET\ButtonGadgetID
            BOX_get_auth_token()
            BOX_get_account_info()
            BOX_get_account_tree()
            BOX_logout()
          EndIf
      EndSelect
    Else
      Delay(10)
    EndIf
  Until quit=1
  CloseWindow(BOX_NET\WindowID) 
    
EndProcedure

BOX_get_ticket()
BOX_OpenLoginWin()
Gruß, Morty
Benutzeravatar
Thorsten1867
Beiträge: 1360
Registriert: 04.02.2005 15:40
Computerausstattung: [Windows 10 x64] [PB V5.7x]
Wohnort: Kaufbeuren
Kontaktdaten:

AW: GET https ? (Box.net API)

Beitrag von Thorsten1867 »

Schaut super aus.
Ich denke mal, ich werde mich dieses Wochenende nicht langweilen. ;-)
Die XML-Dateien sind mir sehr recht, da ich in meinen Programmen selber viel XML verwende.
Download of PureBasic - Module
Download of PureBasic - Programmes

[Windows 11 x64] [PB V6]

Bild
Benutzeravatar
Thorsten1867
Beiträge: 1360
Registriert: 04.02.2005 15:40
Computerausstattung: [Windows 10 x64] [PB V5.7x]
Wohnort: Kaufbeuren
Kontaktdaten:

Re: GET https ? (Box.net API)

Beitrag von Thorsten1867 »

Ich habe hier mal einen ersten Versuch gestartet.

Code: Alles auswählen

;/ --------------------------------
;/ Zugriff auf Box.net (Cloud)
;/ Nov. 2012 by Thorsten Hoeppner
;/ -------------------------------

; Es muss eine Anwendung generiert werden, um einen API-Key zu erhalten
; https://www.box.com/developers/services/edit/
#BOX_ApiKey =                                              ; {your api key}

#BOX_XML = 0
#BOX_TreeXML = 1

#BOX_ResponseXMLFile = "BoxNet_Response.xml"
#BOX_TreeXMLFile = "BoxNet_Tree.xml"

Enumeration
  #BoxAPI_Ticket
  #BoxAPI_AuthToken
  #BoxAPI_AccountInfo
  #BoxAPI_AccountTree
  #BoxAPI_Logout
EndEnumeration


; --- Strukturen ---
Structure BoxNet_Structure
  Ticket.s
  AuthToken.s
  LoginStatus.l
EndStructure
Global BoxNet.BoxNet_Structure

Structure BoxNetUser_Structure
  login.s
  email.s
  access_id.s
  user_id.s
  space_amount.s
  space_used.s
  max_upload_size.s
  sharing_disabled.s
EndStructure
Global BoxNetUser.BoxNetUser_Structure

InitNetwork()

;- --- Fensterdefinitionen ---

; Konstanten ggf. für eigenes Programm anpassen
#Window_BoxNet_Login = 1
#Gadget_BoxNet_Login_WebGadget = 1

Procedure.l Window_BoxNet_Login(url.s)
  If OpenWindow(#Window_BoxNet_Login,82,191,530,580," Anmelden bei Box.net",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_Invisible)
    WebGadget(#Gadget_BoxNet_Login_WebGadget,0,0,520,580,url)
    HideWindow(#Window_BoxNet_Login,0)
    ProcedureReturn WindowID(#Window_BoxNet_Login)
  EndIf
EndProcedure


;- Allgemeine Prozeduren

Procedure ReadResponseXML(type.l)
  
  Select type ;{ Art der Abfrage
    Case #BoxAPI_Ticket
      StatusOK.s = "get_ticket_ok"
    Case #BoxAPI_AuthToken
      StatusOK.s = "get_auth_token_ok"
    Case #BoxAPI_AccountInfo
      StatusOK.s = "get_account_info_ok"
    Case #BoxAPI_AccountTree
      StatusOK.s = "listing_ok"
    Case #BoxAPI_Logout
      StatusOK.s = "logout_ok"
  EndSelect ;}
  
  If LoadXML(#BOX_XML, #BOX_ResponseXMLFile)
    *Response = MainXMLNode(#BOX_XML)      
    If *Response
      *Status = XMLNodeFromPath(*Response, "status")
      If *Status
        If GetXMLNodeText(*Status) = StatusOK
          Debug "-> Abfrage erfolgreich!"
          Select type ; Auswerten der Response-Datei
            Case #BoxAPI_Ticket ;{
              *Node = XMLNodeFromPath(*Response, "ticket")
              If *Node
                BoxNet\Ticket = GetXMLNodeText(*Node)
              EndIf ;}
            Case #BoxAPI_AuthToken ;{
              ; Token ermitteln
              *Node = XMLNodeFromPath(*Response, "auth_token")
              If *Node
                BoxNet\AuthToken = GetXMLNodeText(*Node)
                BoxNet\LoginStatus = #True
              EndIf
              ; User-Daten auslesen
              *Node = XMLNodeFromPath(*Response, "user")
              If *Node
                *ChildNode = ChildXMLNode(*Node) 
                While *ChildNode <> 0
                  Select GetXMLNodeName(*ChildNode) 
                    Case "login"
                      BoxNetUser\login = GetXMLNodeText(*ChildNode)
                    Case "email"
                      BoxNetUser\email = GetXMLNodeText(*ChildNode)
                    Case "access_id"
                      BoxNetUser\access_id = GetXMLNodeText(*ChildNode)
                    Case "user_id"
                      BoxNetUser\user_id = GetXMLNodeText(*ChildNode)
                    Case "space_amount"
                      BoxNetUser\space_amount = GetXMLNodeText(*ChildNode)
                    Case "space_used"
                      BoxNetUser\space_used = GetXMLNodeText(*ChildNode)
                    Case "max_upload_size"
                      BoxNetUser\max_upload_size = GetXMLNodeText(*ChildNode)
                    Case "sharing_disabled"
                      BoxNetUser\sharing_disabled = GetXMLNodeText(*ChildNode)
                  EndSelect
                  *ChildNode = NextXMLNode(*ChildNode)
                Wend 
              EndIf
              ;}
            Case #BoxAPI_AccountInfo ;{
              *Node = XMLNodeFromPath(*Response, "user")
              If *Node
                *ChildNode = ChildXMLNode(*Node) 
                While *ChildNode <> 0
                  Select GetXMLNodeName(*ChildNode) 
                    Case "login"
                      BoxNetUser\login = GetXMLNodeText(*ChildNode)
                    Case "email"
                      BoxNetUser\email = GetXMLNodeText(*ChildNode)
                    Case "access_id"
                      BoxNetUser\access_id = GetXMLNodeText(*ChildNode)
                    Case "user_id"
                      BoxNetUser\user_id = GetXMLNodeText(*ChildNode)
                    Case "space_amount"
                      BoxNetUser\space_amount = GetXMLNodeText(*ChildNode)
                    Case "space_used"
                      BoxNetUser\space_used = GetXMLNodeText(*ChildNode)
                    Case "max_upload_size"
                      BoxNetUser\max_upload_size = GetXMLNodeText(*ChildNode)
                  EndSelect
                  *ChildNode = NextXMLNode(*ChildNode)
                Wend 
              EndIf ;}
            Case #BoxAPI_AccountTree ;{
              *Tree = XMLNodeFromPath(*Response, "tree")
              ; AccountTree in neue XML kopieren
              If CreateXML(#BOX_TreeXML)
                *MainNode = CreateXMLNode(RootXMLNode(#BOX_TreeXML))
                If *MainNode
                  SetXMLNodeName(*MainNode, "BoxNet")
                  CopyXMLNode(*Tree, *MainNode)
                EndIf
              EndIf
              ;}
            Case #BoxAPI_Logout ;{
              BoxNet\LoginStatus = #False
              ;}
          EndSelect
          Status = #True
        Else 
          Debug "Error: "+GetXMLNodeText(*Status)
          Status = #False
        EndIf
      EndIf
    EndIf
    FreeXML(#BOX_XML)
  EndIf
  
  ProcedureReturn Status
EndProcedure

Procedure BoxNet_CloseAccountTree()
  ; XML mit AccoutTree schließen
  If IsXML(#BOX_TreeXML)
    ; --- XML speichern (Debug) ---
    SaveXML(#BOX_TreeXML, #BOX_TreeXMLFile) 
    ; -----------------------------
    FreeXML(#BOX_TreeXML)
  EndIf
EndProcedure

Procedure BoxNet_Logout() 
  If ReceiveHTTPFile("https://www.box.net/api/1.0/rest?action=logout&api_key="+#BOX_ApiKey+"&auth_token="+BoxNet\AuthToken, #BOX_ResponseXMLFile)
    If ReadResponseXML(#BoxAPI_Logout)
      Debug "-> Logout"
      ProcedureReturn #True
    EndIf
  EndIf
  ProcedureReturn #False
EndProcedure


;- Allgemeine Zugriffe auf Box.net

Procedure BoxAPI_GetAccountInfo()
  ; https://www.box.net/api/1.0/rest?action=get_account_info&api_key={your api key}&auth_token={your auth token}
  If ReceiveHTTPFile("https://www.box.net/api/1.0/rest?action=get_account_info&api_key="+#BOX_ApiKey+"&auth_token="+BoxNet\AuthToken, #BOX_ResponseXMLFile)
    If ReadResponseXML(#BoxAPI_AccountInfo)
      ProcedureReturn #True
    EndIf
  EndIf
  ProcedureReturn #False
EndProcedure

Procedure BoxAPI_GetAccountTree(folder.l=0)
  ; https://www.box.net/api/1.0/rest?action=get_account_tree&api_key={your api key}&auth_token={your auth token}&folder_id=0&params[]=onelevel&params[]=nozip
  If ReceiveHTTPFile("https://www.box.net/api/1.0/rest?action=get_account_tree&api_key="+#BOX_ApiKey+"&auth_token="+BoxNet\AuthToken+"&folder_id="+Str(folder)+"&params[]=onelevel&params[]=nozip", #BOX_ResponseXMLFile)
    If ReadResponseXML(#BoxAPI_AccountTree)
      ProcedureReturn #True
    EndIf
  EndIf
  ProcedureReturn #False
EndProcedure


;- AuthToken für Zugriff auf Box.net anfordern

Procedure BoxAPI_GetTicket() ; Neues Ticket anfordern (Gültigkeit: 10 min.) 
  ; GET https://www.box.com/api/1.0/rest?action=get_ticket&api_key={your api key}  
  If ReceiveHTTPFile("https://www.box.net/api/1.0/rest?action=get_ticket&api_key="+#BOX_ApiKey, #BOX_ResponseXMLFile)
    If ReadResponseXML(#BoxAPI_Ticket)
      ProcedureReturn #True
    EndIf
  EndIf
  ProcedureReturn #False
EndProcedure

Procedure BoxAPI_GetAuthToken() ; Retrieve Auth_Token after login
  ; GET https://www.box.com/api/1.0/rest?action=get_auth_token&api_key={your api key}&ticket={your ticket}
  If ReceiveHTTPFile("https://www.box.net/api/1.0/rest?action=get_auth_token&api_key="+#BOX_ApiKey+"&ticket="+BoxNet\Ticket, #BOX_ResponseXMLFile)
    If ReadResponseXML(#BoxAPI_AuthToken)
      ProcedureReturn #True
    EndIf
  EndIf
  ProcedureReturn #False 
EndProcedure


Procedure BoxNet_Login()
  ; https://www.box.com/api/1.0/auth/{your ticket}
  BoxAPI_GetTicket() ; Ticket für AuthToken-Anforderung
  If Window_BoxNet_Login("https://m.box.com/api/1.0/auth/"+BoxNet\Ticket)
    quitBoxNet_Login=0
    Repeat 
      If WaitWindowEvent() = #PB_Event_CloseWindow
        If EventWindow()=#Window_BoxNet_Login
          quitBoxNet_Login=1
        EndIf
      EndIf
    Until quitBoxNet_Login
    CloseWindow(#Window_BoxNet_Login)
    BoxAPI_GetAuthToken() ; AuthToken anfordern
  EndIf
EndProcedure


;- ===== Test Box.net ==============================

BoxNet_Login()

msg$ = "Login: "+BoxNetUser\login + #LF$ + "E-Mail: "+BoxNetUser\email + #LF$ + "Speichergröße: "+BoxNetUser\space_amount + #LF$ + "Benutzter Speicher: "+BoxNetUser\space_used
MessageRequester(" Box.net - Account Informationen", msg$, #MB_OK|#MB_ICONINFORMATION)

If BoxAPI_GetAccountTree()
  If IsXML(#BOX_TreeXML)
    msg$ = "Dateien:"+#LF$+#LF$
    *MainNode = MainXMLNode(#BOX_TreeXML)
    If *MainNode
      *Node = XMLNodeFromPath(*MainNode, "tree/folder/files")
      If *Node
        *ChildNode = ChildXMLNode(*Node) 
        While *ChildNode <> 0
          msg$ + GetXMLAttribute(*ChildNode, "file_name") + #LF$
          *ChildNode = NextXMLNode(*ChildNode)
        Wend 
        MessageRequester(" Box.net - Verzeichnis", msg$, #MB_OK|#MB_ICONINFORMATION)
      EndIf
    EndIf
  EndIf 
  BoxNet_CloseAccountTree() ; XML mit AccountTree schließen
EndIf

BoxNet_Logout() ; Box.net abmelden
Download of PureBasic - Module
Download of PureBasic - Programmes

[Windows 11 x64] [PB V6]

Bild
Benutzeravatar
Thorsten1867
Beiträge: 1360
Registriert: 04.02.2005 15:40
Computerausstattung: [Windows 10 x64] [PB V5.7x]
Wohnort: Kaufbeuren
Kontaktdaten:

Re: GET https ? (Box.net API)

Beitrag von Thorsten1867 »

Ich habe den Code auf die Verwendung von WinHTTP angepasst.
Hiermit ist es möglich die Response-XML direkt aus dem Speicher zu laden, anstatt den Umweg über eine XML-Datei auf der Festplatte zu gehen. Zumdem unterstützt WinHTTP offenbar auch den Request-Type "POST".

Bisher realisierte Zugriffe auf die Box.net API:
  • - Login via Web-Interface (=> AuthToken)
    - Account-Informationen abfragen
    - Verzeichnisinhalt auslesen (=> XML oder LinkedList)
    - Ordner erstellen
    - Download einer Datei
    - Upload einer Datei
    - Logout

Code: Alles auswählen

;/ --------------------------------
;/ Zugriff auf Box.net mittels WinHttp
;/ Nov. 2012 by Thorsten Hoeppner
;/ -------------------------------

; Tool für Upload: curl.exe -> http://curl.haxx.se/download.html

; Es muss eine Anwendung generiert werden, um einen API-Key zu erhalten
; https://www.box.com/developers/services/edit/
#BOX_ApiKey = {your api key}

#BOX_XML = 0
#BOX_TreeXML = 1
#BOX_FileID = 0

#BOX_ResponseXMLFile = "BoxNet_Response.xml"
;#BOX_UploadHTML = "BoxNet_UploadHTML.htm"
#BOX_TreeXMLFile = "BoxNet_Tree.xml"

#BOX_File = 1
#BOX_Folder = 2

Enumeration
  #BoxAPI_GetTicket
  #BoxAPI_GetAuthToken
  #BoxAPI_GetAccountInfo
  #BoxAPI_GetAccountTree
  #BoxAPI_CreateFolder
  #BoxAPI_Logout
EndEnumeration

IncludeFile "WinHTTP.pbi" ; => http://www.purebasic.fr/german/viewtopic.php?f=8&t=19078&hilit=winhttp&start=10

; --- Strukturen ---
Structure BoxNet_Structure
  Ticket.s
  AuthToken.s
  LoginStatus.l
  FolderID.l
  StatusMsg.s
EndStructure
Global BoxNet.BoxNet_Structure

Structure BoxNetUser_Structure
  login.s
  email.s
  access_id.s
  user_id.s
  space_amount.s
  space_used.s
  max_upload_size.s
  sharing_disabled.s
EndStructure
Global BoxNetUser.BoxNetUser_Structure

Structure Params_Structure
  FolderID.l
  FileID.l
  Name.s
  Share.l
EndStructure
Global Params.Params_Structure

Structure BoxTree_Structure
  EntryType.l
  Name.s
  ID.s
EndStructure


InitNetwork()

If WinHttp_LoadDLL() = #False ;{ WinHTTP einbinden
  MessageRequester(" WinHTTP", "Fehler beim Laden der DLL!", #MB_OK|#MB_ICONERROR)
  End
EndIf ;}


;- --- Fensterdefinitionen ---

; Konstanten ggf. für eigenes Programm anpassen
#Window_BoxNet_Login = 1
#Gadget_BoxNet_Login_WebGadget = 1

Procedure.l Window_BoxNet_Login(url.s)
  If OpenWindow(#Window_BoxNet_Login,82,191,540,580," Anmelden bei Box.net",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_Invisible)
    WebGadget(#Gadget_BoxNet_Login_WebGadget,0,0,540,580,url)
    HideWindow(#Window_BoxNet_Login,0)
    ProcedureReturn WindowID(#Window_BoxNet_Login)
  EndIf
EndProcedure


;- Allgemeine Zugriffe auf Box.net API

Procedure Box_GetTreeData(List BoxTree.BoxTree_Structure()) ; AccountTree auslesen und schließen
  If IsXML(#BOX_TreeXML)
    *MainNode = MainXMLNode(#BOX_TreeXML)
    If *MainNode
      ;{ --- Ordner ---
      *Node = XMLNodeFromPath(*MainNode, "tree/folder/folders")
      If *Node
        *ChildNode = ChildXMLNode(*Node) 
        While *ChildNode
          AddElement(BoxTree())
          BoxTree()\EntryType = #BOX_Folder
          BoxTree()\Name = GetXMLAttribute(*ChildNode, "name")
          BoxTree()\ID = GetXMLAttribute(*ChildNode, "id")
          *ChildNode = NextXMLNode(*ChildNode)
        Wend 
      EndIf ;}
      ;{ --- Dateien ---
      *Node = XMLNodeFromPath(*MainNode, "tree/folder/files")
      If *Node
        *ChildNode = ChildXMLNode(*Node) 
        While *ChildNode
          AddElement(BoxTree())
          BoxTree()\EntryType = #BOX_File
          BoxTree()\Name = GetXMLAttribute(*ChildNode, "file_name")
          BoxTree()\ID = GetXMLAttribute(*ChildNode, "id")
          *ChildNode = NextXMLNode(*ChildNode)
        Wend 
      EndIf ;}
    EndIf
    FreeXML(#BOX_TreeXML)
  EndIf
EndProcedure

Procedure Box_CloseAccountTree() ; XML mit AccoutTree schließen
  If IsXML(#BOX_TreeXML)
    ; --- XML speichern (Debug) ---
    SaveXML(#BOX_TreeXML, #BOX_TreeXMLFile) 
    ; -----------------------------
    FreeXML(#BOX_TreeXML)
  EndIf
EndProcedure



Procedure Box_GetUserData(*User) ; Userdaten -> BoxNetUser-Structure
  If IsXML(#BOX_XML)
    *ChildNode = ChildXMLNode(*User) 
    While *ChildNode <> 0
      Select GetXMLNodeName(*ChildNode) 
        Case "login"
          BoxNetUser\login = GetXMLNodeText(*ChildNode)
        Case "email"
          BoxNetUser\email = GetXMLNodeText(*ChildNode)
        Case "access_id"
          BoxNetUser\access_id = GetXMLNodeText(*ChildNode)
        Case "user_id"
          BoxNetUser\user_id = GetXMLNodeText(*ChildNode)
        Case "space_amount"
          BoxNetUser\space_amount = GetXMLNodeText(*ChildNode)
        Case "space_used"
          BoxNetUser\space_used = GetXMLNodeText(*ChildNode)
        Case "max_upload_size"
          BoxNetUser\max_upload_size = GetXMLNodeText(*ChildNode)
        Case "sharing_disabled"
          BoxNetUser\sharing_disabled = GetXMLNodeText(*ChildNode)
      EndSelect
      *ChildNode = NextXMLNode(*ChildNode)
    Wend 
  EndIf
EndProcedure

Procedure BoxAPI_Call(action.l)
  Define.s url, StatusOK, RequestType.s="GET"
  
  Select action ;{ Art der Abfrage
    Case #BoxAPI_GetTicket
      url = "https://www.box.net/api/1.0/rest?action=get_ticket&api_key="+#BOX_ApiKey
      StatusOK = "get_ticket_ok"
    Case #BoxAPI_GetAuthToken
      url = "https://www.box.net/api/1.0/rest?action=get_auth_token&api_key="+#BOX_ApiKey+"&ticket="+BoxNet\Ticket
      StatusOK = "get_auth_token_ok"
    Case #BoxAPI_GetAccountInfo
      url = "https://www.box.net/api/1.0/rest?action=get_account_info&api_key="+#BOX_ApiKey+"&auth_token="+BoxNet\AuthToken
      StatusOK = "get_account_info_ok"
    Case #BoxAPI_GetAccountTree
      url = "https://www.box.net/api/1.0/rest?action=get_account_tree&api_key="+#BOX_ApiKey+"&auth_token="+BoxNet\AuthToken+"&folder_id="+Str(Params\FolderID)+"&Params[]=onelevel&Params[]=nozip"
      StatusOK = "listing_ok"
    Case #BoxAPI_CreateFolder
      url = "https://www.box.net/api/1.0/rest?action=create_folder&api_key="+#BOX_ApiKey+"&auth_token="+BoxNet\AuthToken+"&parent_id="+Str(Params\FolderID)+"&Name="+Params\Name+"&share="+Str(Params\Share)
      StatusOK = "create_ok"
    Case #BoxAPI_Logout
      url = "https://www.box.net/api/1.0/rest?action=logout&api_key="+#BOX_ApiKey+"&auth_token="+BoxNet\AuthToken
      StatusOK = "logout_ok"
  EndSelect ;}
  
  *XML = ReceiveHTTPMemory(url, RequestType)
  If *XML
    If CatchXML(#BOX_XML, *XML, MemorySize(*XML))
      *Response = MainXMLNode(#BOX_XML)      
      If *Response
        *Status = XMLNodeFromPath(*Response, "status")
        If *Status
          BoxNet\StatusMsg = GetXMLNodeText(*Status)
          Debug "Statusmeldung: "+BoxNet\StatusMsg
          If BoxNet\StatusMsg = StatusOK
            Select action ; Auswerten der Response-Datei
              Case #BoxAPI_GetTicket ;{
                *Node = XMLNodeFromPath(*Response, "ticket")
                If *Node
                  BoxNet\Ticket = GetXMLNodeText(*Node)
                EndIf ;}
              Case #BoxAPI_GetAuthToken ;{
                *Node = XMLNodeFromPath(*Response, "auth_token")
                If *Node
                  BoxNet\AuthToken = GetXMLNodeText(*Node)
                  BoxNet\LoginStatus = #True
                EndIf
                *Node = XMLNodeFromPath(*Response, "user")
                If *Node
                  Box_GetUserData(*Node) 
                EndIf
                ;}
              Case #BoxAPI_GetAccountInfo ;{
                *Node = XMLNodeFromPath(*Response, "user")
                If *Node
                  Box_GetUserData(*Node)
                EndIf ;}
              Case #BoxAPI_GetAccountTree ;{
                *Tree = XMLNodeFromPath(*Response, "tree")
                ; AccountTree in neue XML kopieren
                If CreateXML(#BOX_TreeXML)
                  *MainNode = CreateXMLNode(RootXMLNode(#BOX_TreeXML))
                  If *MainNode
                    SetXMLNodeName(*MainNode, "BoxNet")
                    CopyXMLNode(*Tree, *MainNode)
                  EndIf
                EndIf
                ;}
              Case #BoxAPI_CreateFolder ;{
                *Node = XMLNodeFromPath(*Response, "folder/folder_id")
                If *Node
                  BoxNet\FolderID = Val(GetXMLNodeText(*Node))
                EndIf ;}
              Case #BoxAPI_Logout ;{
                BoxNet\LoginStatus = #False
                ;}
            EndSelect
            Status = #True
          Else
            Status = #False
          EndIf
        EndIf
      EndIf
      FreeXML(#BOX_XML)
    EndIf
  EndIf
  ProcedureReturn Status
EndProcedure


;- Box.net API Calls

Procedure BoxAPI_GetTicket() ; Neues Ticket anfordern (Gültigkeit: 10 min.) 
  ; https://www.box.com/api/1.0/rest?action=get_ticket&api_key={your api key}  
  If BoxAPI_Call(#BoxAPI_GetTicket)
    ProcedureReturn #True
  Else
    MessageRequester(" Box.net - Status", BoxNet\StatusMsg, #MB_OK|#MB_ICONWARNING)
    ProcedureReturn #False 
  EndIf
EndProcedure

Procedure BoxAPI_GetAuthToken() ; AuthToken anfordern (nach erfolgreichem Login)
  ; https://www.box.com/api/1.0/rest?action=get_auth_token&api_key={your api key}&ticket={your ticket}
  If BoxAPI_Call(#BoxAPI_GetAuthToken)
    ProcedureReturn #True
  Else
    MessageRequester(" Box.net - Status", BoxNet\StatusMsg, #MB_OK|#MB_ICONWARNING)
    ProcedureReturn #False 
  EndIf
EndProcedure

Procedure BoxAPI_GetAccountInfo() ; Account Informationen anfordern
  ; https://www.box.net/api/1.0/rest?action=get_account_info&api_key={your api key}&auth_token={your auth token}
  If BoxAPI_Call(#BoxAPI_GetAccountInfo)
    ProcedureReturn #True
  Else
    MessageRequester(" Box.net - Status", BoxNet\StatusMsg, #MB_OK|#MB_ICONWARNING)
    ProcedureReturn #False 
  EndIf
EndProcedure

Procedure BoxAPI_GetAccountTree(folder.l=0) ; AccountTree anfordern
  ; https://www.box.net/api/1.0/rest?action=get_account_tree&api_key={your api key}&auth_token={your auth token}&folder_id=0&params[]=onelevel&params[]=nozip
  Params\FolderID = folder
  If BoxAPI_Call(#BoxAPI_GetAccountTree)
    ProcedureReturn #True
  Else
    MessageRequester(" Box.net - Status", BoxNet\StatusMsg, #MB_OK|#MB_ICONWARNING)
    ProcedureReturn #False 
  EndIf
EndProcedure

Procedure BoxAPI_Logout() ; Ausloggen aus Box.net
  ; https://www.box.net/api/1.0/rest?action=logout&api_key={your api key}&auth_token={your auth token}
  If BoxAPI_Call(#BoxAPI_Logout)
    ProcedureReturn #True
  Else
    ;MessageRequester(" Box.net - Status", BoxNet\StatusMsg, #MB_OK|#MB_ICONWARNING)
    ProcedureReturn #False 
  EndIf
EndProcedure


Procedure BoxAPI_Download(FileName.s, FileID.s) ; Datei downloaden
  ; https://www.box.net/api/1.0/download/{your auth token}/{file_id}
  If ReceiveHTTPFileEx("https://www.box.net/api/1.0/download/"+BoxNet\AuthToken+"/"+FileID, FileName)
    Debug "Download: "+FileName
    ProcedureReturn #True
  EndIf
  ProcedureReturn #False
EndProcedure

Procedure BoxAPI_CreateFolder(FolderName.s, ParentFolder.l=0, Share.l=0) ; Ordner erstellen
  ; https://www.box.net/api/1.0/rest?action=create_folder&api_key={your api key}&auth_token={your auth token}&parent_id={folder_id}&name={folder name}&share={0/1}
  Params\Name = ReplaceString(FolderName, " ", "%20") ; Ordnername
  Params\FolderID = ParentFolder ; übergeordneter Ordner
  Params\Share = Share
  If BoxAPI_Call(#BoxAPI_CreateFolder)
    ProcedureReturn BoxNet\FolderID ; ID des erstellten Ordners
  Else
    MessageRequester(" Box.net - Status", BoxNet\StatusMsg, #MB_OK|#MB_ICONWARNING)
    ProcedureReturn #False 
  EndIf
EndProcedure


Procedure BoxAPI_Upload(FileName.s, FolderID.s="0")
  ; curl -k https://www.box.com/api/2.0/files/content/ -H "Authorization: BoxAuth api_key=API_KEY&auth_token=AUTH_TOKEN" -F FileName=@"c:\alarms.gif" -F folder_id=folder_id
  param$ = "-k https://www.box.com/api/2.0/files/content/ -H "+Chr(34)+"Authorization: BoxAuth api_key="+#BOX_ApiKey+"&auth_token="+BoxNet\AuthToken+Chr(34)+" -F FileName=@"+FileName+" -F folder_id="+FolderID
  RunPrg = RunProgram("curl.exe", param$, "", #PB_Program_Open|#PB_Program_Hide)
  If RunPrg
    WaitProgram(RunPrg, 30000) ; max. 30s warten
    If ProgramRunning(RunPrg) = #False 
      ExitCode = ProgramExitCode(RunPrg)
    EndIf
    CloseProgram(RunPrg)
  EndIf
  ProcedureReturn ExitCode
EndProcedure


;- Box.net Anmeldung

Procedure Box_WebLogin() ; Login über WebInterface
  ; https://www.box.com/api/1.0/auth/{your ticket}
  BoxAPI_GetTicket()
  If Window_BoxNet_Login("https://m.box.com/api/1.0/auth/"+BoxNet\Ticket)
    quitBoxNet_Login=0
    Repeat 
      If WaitWindowEvent() = #PB_Event_CloseWindow
        If EventWindow()=#Window_BoxNet_Login
          quitBoxNet_Login=1
        EndIf
      EndIf
    Until quitBoxNet_Login
    CloseWindow(#Window_BoxNet_Login)
    If BoxAPI_GetAuthToken()
      ProcedureReturn #True
    EndIf
  EndIf
  ProcedureReturn #False
EndProcedure


;- ===== Box.net API testen =====

If Box_WebLogin() ; Anmeldung bei Box.net
  msg$ = "Login: "+BoxNetUser\login + #LF$ + "E-Mail: "+BoxNetUser\email + #LF$ + "Speichergröße: "+BoxNetUser\space_amount + #LF$ + "Benutzter Speicher: "+BoxNetUser\space_used
  MessageRequester(" Box.net - Account Informationen", msg$, #MB_OK|#MB_ICONINFORMATION)
EndIf

; Dateien aus dem Hauptverzeichnis von Box.net anzeigen
NewList BoxTree.BoxTree_Structure()

If BoxAPI_GetAccountTree() 
  Box_GetTreeData(BoxTree())
  msg$="Verzeichnisinhalt:"+#LF$
  ForEach BoxTree()
    Select BoxTree()\EntryType
      Case #BOX_Folder
        msg$+"["+BoxTree()\Name+"]"+#LF$
      Case #BOX_File
        msg$+BoxTree()\Name+#LF$
    EndSelect
  Next
  MessageRequester(" Box.net - Verzeichnis", msg$, #MB_OK|#MB_ICONINFORMATION)
EndIf

FolderID = BoxAPI_CreateFolder("Test")
Debug "FolderID: "+Str(FolderID)

If ListSize(BoxTree()) ; letzten Eintrag laden (falls vorhanden)
  If BoxTree()\EntryType = #BOX_File
    BoxAPI_Download(BoxTree()\Name, BoxTree()\ID)
    Debug "Download: "+BoxTree()\Name
  EndIf
EndIf

File$ = "C:\Test.jpg"
If BoxAPI_Upload(File$)
  Debug "Upload: "+File$
EndIf

BoxAPI_Logout() ; Box.net abmelden
Leider ist es mir nur gelungen Datei-Uploads mittels eines externen Tools (curl.exe) zu realisieren. Falls jemand eine bessere Lösung weiß, bitte melden!
Download of PureBasic - Module
Download of PureBasic - Programmes

[Windows 11 x64] [PB V6]

Bild
Antworten