I'm new to Python and I am trying to write a simple script using the Xbox api and this Python wrapper.
I successfully installed the xboxapi wrapper but when I tried to test it with this simple code from the wrapper's Github page I get an error.
Code:
from xboxapi import XboxApi
api = XboxApi(api_key="Your API key")
profile_info = api.get_profile()
print(profile_info)
Error:
Traceback (most recent call last):
File "E:\ProgramFiles\Python 3.6\Scripts\xboxTest.py", line 1, in <module>
from xboxapi import XboxApi
ImportError: cannot import name 'XboxApi'
If I understand this error correctly then it appears that the xboxapi package is recognized but the class XboxApi is not. It does however contain and XboxApi class in the code.
# -*- coding: utf-8 -*-
import requests
import json
class XboxApi:
# XboxApi key
api_key = ""
def __init__(self, api_key):
"""Only requires the XboxApi key"""
self.api_key = api_key
def get_profile(self):
"""Return information for current token profile"""
res = self.request("https://xboxapi.com/v2/profile")
return res.json()
def get_xuid(self):
"""Return your xuid"""
res = self.request("https://xboxapi.com/v2/accountXuid")
return res.json()
def get_messages(self):
"""Return your messages"""
res = self.request("https://xboxapi.com/v2/messages")
return res.json()
def get_conversations(self):
"""Return your messages"""
res = self.request("https://xboxapi.com/v2/conversations")
return res.json()
def get_xuid_by_gamertag(self, gamertag):
"""Return XUID by gamertag"""
res = self.request("https://xboxapi.com/v2/xuid/{}".format(gamertag))
return res.json()
def get_gamertag_by_xuid(self, xuid):
"""Return gamertag by XUID"""
res = self.request("https://xboxapi.com/v2/gamertag/{}".format(xuid))
return res.json()
def get_user_profile(self, xuid):
"""Return profile by XUID"""
res = self.request("https://xboxapi.com/v2/{}/profile".format(xuid))
return res.json()
def get_user_gamercard(self, xuid):
"""Return gamercard by XUID"""
res = self.request("https://xboxapi.com/v2/{}/gamercard".format(xuid))
return res.json()
def get_user_presence(self, xuid):
"""Return current presence information by XUID"""
res = self.request("https://xboxapi.com/v2/{}/presence".format(xuid))
return res.json()
def get_user_activity(self, xuid):
"""Return current activity information by XUID"""
res = self.request("https://xboxapi.com/v2/{}/activity".format(xuid))
return res.json()
def get_user_activity_recent(self, xuid):
"""Return recent activity information by XUID"""
res = self.request("https://xboxapi.com/v2/{}/activity/recent".format(xuid))
return res.json()
def get_user_friends(self, xuid):
"""Return friends by XUID"""
res = self.request("https://xboxapi.com/v2/{}/friends".format(xuid))
return res.json()
def get_user_followers(self, xuid):
"""Return followers by XUID"""
res = self.request("https://xboxapi.com/v2/{}/followers".format(xuid))
return res.json()
def get_recent_players(self):
"""Return recent players by XUID"""
res = self.request("https://xboxapi.com/v2/recent-players")
return res.json()
def get_user_gameclips(self, xuid):
"""Return game clips by XUID"""
res = self.request("https://xboxapi.com/v2/{}/game-clips".format(xuid))
return res.json()
# continue with #16
def send_message(self, message, xuids=[]):
"""Send a message to a set of user(s)"""
headers = {
"X-AUTH": self.api_key,
"Content-Type": "application/json"
}
payload = {
"message": message,
"to": []
}
for xuid in xuids:
payload["to"].append(xuid)
res = requests.post("https://xboxapi.com/v2/messages", headers=headers, data=json.dumps(payload))
res.json()
def request(self, url):
"""Wrapper on the requests.get"""
headers = {"X-AUTH": self.api_key}
res = requests.get(url, headers=headers)
return res
Is there something simple I am missing?
The xboxapi is a package so when you do
from xboxapi import ZooLander
You're trying to import the ZooLander class from the xboxapi package. The problem is, the ZooLander class is not in the xboxapi package, it's in a module (Python parlance for "file") inside of the package. There are a couple of ways around this. You could import the module and use dot notation to refer to the class:
from xboxapi import StillerMovies
z = StillerMovies.ZooLander()
Or you could import the class directly like this:
from xboxapi.StillerMovies import ZooLander
z = ZooLander()
The above assume that the class ZooLander is in a file called StillerMovies.py at the top of the xboxapi package.