2016-07-14 66 views
0

我似乎无法获取嵌入-API服务器端授权的演示工作: https://ga-dev-tools.appspot.com/embed-api/server-side-authorization/嵌入-API,谷歌Analytics(分析),服务器端授权

在演示中,它说以下内容:

安装库之后,您可以将以下python模块 添加到您的项目中,并调用get_access_token()方法获取可用于授权Embed API的 访问令牌。

# service-account.py 
from oauth2client.service_account import ServiceAccountCredentials 
# The scope for the OAuth2 request. 
SCOPE = 'https://www.googleapis.com/auth/analytics.readonly' 
# The location of the key file with the key data. 
KEY_FILEPATH = 'path/to/json-key.json' 
# Defines a method to get an access token from the ServiceAccount object. 
def get_access_token(): 
return ServiceAccountCredentials.from_json_keyfile_name(
KEY_FILEPATH, SCOPE).get_access_token().access_token 

我已经成功地完成了所有前面的步骤,但是这一个我不能左右我的头。我在哪里放这个代码?它好像应该放在一个.py文件中。

有人可以帮忙吗?

回答

0

这取决于你的实现,但基本上你想在你的服务器上运行你的服务账户代码,并将访问令牌传递给你的客户端应用程序,以便它可以从浏览器发出授权请求。

整个应用程序是开源的,您可以看到服务帐户代码位于source code的哪个位置。

就像在演示中一样,如果您使用的是django或app引擎,则很容易在您的站点中放置python服务器代码,它将返回令牌并替换template code中的值。

+0

谢谢你马特。我只想在我的网站上创建一个仪表板:http://web-tailor.nl/dashboard/这个仪表板现在可以工作,但我希望它有一个固定的视图,无需登录。是否有一种方法可以实现这个?我有点小菜,但我没有服务器。 – PaulusfranCircus

+0

如果没有服务器,将无法使用[服务帐户](https://developers.google.com/identity/protocols/OAuth2ServiceAccount)。仔细研究各种[OAuth 2.0场景](https://developers.google.com/identity/protocols/OAuth2#scenarios)。服务帐户需要一个安全的地方运行。 – Matt

0

将该代码添加到service-account.py文件中,并使用FTP将它上载到您的服务器上。我救了使用Dreamweaver的代码,更新的路径,并在service-account.py文件的末尾添加以下行:

print get_access_token() 

在同一目录下上传上传.json文件,并运行命令python service-account.py得到access_token

+0

从Python 3开始,您需要执行print(get_access_token())https://stackoverflow.com/a/937516/1082555 –

相关问题