gcloud auth print-access-token给我一个Bearer令牌,以后可以使用;但是,这是一个shell命令。如何通过Google Cloud Python API以编程方式获取一个?
我看到了使用oauth2client的先前示例,但现在已弃用。如何使用google.auth和oauthlib做到这一点?oauth2client
我正在尝试使用Google Cloud Translate API。我从服务帐户生成了JSON文件,并将设置GOOGLE_APPLICATION_CREDENTIALS为JSON文件的保存位置。然后,我在这样的程序中使用了它:
import com.google.cloud.translate.*;
...
Translate translate = TranslateOptions.getDefaultInstance().getService();
Translation translation = translate.translate(message);
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误
com.google.cloud.translate.TranslateException: Your application has authenticated using end user credentials from the Google Cloud SDK or Google Cloud Shell which are not supported by the translate.googleapis.com. We recommend that most server applications use service accounts instead. For more information about service accounts and how to use them in your application, see https://cloud.google.com/docs/authentication/
at com.google.cloud.translate.spi.v2.HttpTranslateRpc.translate(HttpTranslateRpc.java:61)
at com.google.cloud.translate.spi.v2.HttpTranslateRpc.translate(HttpTranslateRpc.java:144)
at com.google.cloud.translate.TranslateImpl$4.call(TranslateImpl.java:113)
at com.google.cloud.translate.TranslateImpl$4.call(TranslateImpl.java:110)
at com.google.api.gax.retrying.DirectRetryingExecutor.submit(DirectRetryingExecutor.java:89)
at …Run Code Online (Sandbox Code Playgroud) java authentication json google-translate google-cloud-platform