1

嗨,我正在研究 Google Cloud API,当我尝试运行 GC 文档中给出的演示程序时,代码如下

from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types

# Instantiates a client
client = language.LanguageServiceClient()

# The text to analyze
text = u'Hello, world!'
document = types.Document(
    content=text,
    type=enums.Document.Type.PLAIN_TEXT)

# Detects the sentiment of the text
sentiment = client.analyze_sentiment(document=document).document_sentiment

print('Text: {}'.format(text))
print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude))

遇到此语句时代码不运行 sentiment = client.analyze_sentiment(document=document).document_sentiment

并给出以下长错误

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/google/cloud/language_v1/gapic/language_service_client.py", line 247
, in analyze_sentiment
    request, retry=retry, timeout=timeout, metadata=metadata
  File "/usr/local/lib/python2.7/dist-packages/google/api_core/gapic_v1/method.py", line 143, in __call__
    return wrapped_func(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/google/api_core/retry.py", line 277, in retry_wrapped_func
    on_error=on_error,
  File "/usr/local/lib/python2.7/dist-packages/google/api_core/retry.py", line 202, in retry_target
    last_exc,
  File "/usr/lib/python2.7/dist-packages/six.py", line 718, in raise_from
    raise value
google.api_core.exceptions.RetryError: Deadline of 600.0s exceeded while calling <functools.partial object at 0x7f9
527b06fc8>, last exception: 503 Getting metadata from plugin failed with error: ('Failed to retrieve http://metadat
a.google.internal/computeMetadata/v1/instance/service-accounts/default/?recursive=true from the Google Compute Engi
nemetadata service. Status: 404 Response:\n<!DOCTYPE html>\n<html lang=en>\n  <meta charset=utf-8>\n  <meta name=vi
ewport content="initial-scale=1, minimum-scale=1, width=device-width">\n  <title>Error 404 (Not Found)!!1</title>\n
  <style>\n    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;paddi
ng:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.go
ogle.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{c
olor:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0
;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_col
or_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//w
ww.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-im
age:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-w
ebkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_colo
r_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}\n 
 </style>\n  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>\n  <p><b>404.</b> <ins>That\xe2\
x80\x99s an error.</ins>\n  <p>The requested URL <code>/computeMetadata/v1/instance/service-accounts/default/?recur
sive=true</code> was not found on this server.  <ins>That\xe2\x80\x99s all we know.</ins>\n', <google.auth.transpor
t.requests._Response object at 0x7f9527b22710>)

如何调试此错误任何帮助将不胜感激,谢谢

4

1 回答 1

0

我试图复制这个问题,但我在本地测试中遇到了另一个错误,因为在重试逻辑之后它由于请求超时而失败。然后我在云函数环境中尝试了相同的代码,它工作正常。

我的本地环境是 Cloud Shell,我担心这可能是最近云网络中断造成的问题。

要调试到您的本地环境,您可以使用本教程

于 2019-10-23T22:41:09.217 回答