如何在Android应用中使用Google Translate API并翻译文本?我试过这个:
http://code.google.com/p/google-api-translate-java/
但它不起作用.我也有Api Key.我的代码是(根据官方的例子):
GoogleAPI.setHttpReferrer("http://mysite.com");
GoogleAPI.setKey("My Key");
String translatedText = Translate.DEFAULT.execute("Bonjour le monde", Language.FRENCH, Language.ENGLISH);
Run Code Online (Sandbox Code Playgroud)
我把这个代码放在我的主Activity中,但是我收到了一个错误:"Unhandled exception type GoogleAPIException"并且有一个建议来解决这个错误:"Surrond with Try/Catch".但当我将代码更改为:
try {
String translatedText = Translate.DEFAULT.execute("Bonjour le monde", Language.FRENCH, Language.ENGLISH);
} catch (GoogleAPIException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我的代码运行正常,但我无法得到任何响应(翻译文本).我也设置了这个权限: <uses-permission android:name="android.permission.INTERNET"/>
我的错误在哪里?
java android google-api google-translate google-api-java-client
我正在使用 google-api-java-client 版本 1.8-beta 对 Google 帐户进行 oAuth2 身份验证。一切都很好,直到我得到 GoogleTokenResponse 对象,它有访问令牌但没有刷新令牌。要构建请求 url,我使用以下方法:
...
googleAuthenticationUrl = new GoogleAuthorizationCodeRequestUrl(CLIENT_ID, callBackUrl, scopes).build();
...
Run Code Online (Sandbox Code Playgroud)
获取请求令牌时,我将其与此行中的访问令牌交换:
...
GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(new NetHttpTransport(), new JacksonFactory(), CLIENT_ID, CLIENT_SECRET, request.getParameter(CODE_URL_PARAM), callBackUrl).execute();
...
Run Code Online (Sandbox Code Playgroud)
返回的 GoogleTokenResponse 对象不包含刷新令牌:
{"access_token":"ya29.AH..etc...9-Y","expires_in":3600,"token_type":"Bearer"}
Run Code Online (Sandbox Code Playgroud)
你能解释一下这个问题吗?非常感谢您的帮助!
我正在为OAuth2的应用程序使用Google Apps API.,但是我收到了一个错误.
Error: invalid_client
no support email
Run Code Online (Sandbox Code Playgroud)
请求详细信息这就是我们所知道的.
google-app-engine google-api google-api-java-client google-oauth google-developers-console
我在使用 Google Drive API 上传文件时遇到了一些问题。API 不会返回任何错误并显示文件已成功上传。但是,当我尝试检索文件列表时,它不会出现,当我尝试查看 Google Drive API 页面时也不会出现。
以下是一些观察结果:
这是我的代码的外观(我尝试根据 Java 代码段创建它)。
public class Quickstart {
/** Application name. */
private static final String APPLICATION_NAME = "Drive API Java Quickstart";
/** Directory to store user credentials for this application. */
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
/** Global instance of the HTTP transport. …Run Code Online (Sandbox Code Playgroud) 通过 Android 构建失败并出现以下错误 -
Error:(23, 68) error: package com.google.api.client.googleapis.extensions.android.gms.auth does not exist
Error:(60, 20) error: cannot find symbol class GoogleAccountCredential
Error:(79, 19) error: cannot find symbol class GoogleAccountCredential
Error:(218, 22) error: cannot find symbol variable GoogleAccountCredential
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚这个类属于哪个库。包是
com.google.api.client.googleapis.extensions.android.gms.authc.GoogleAccountCredential;
我尝试在构建依赖项中添加所有库。现在看起来像下面 -
dependencies {
compile 'com.google.android.gms:play-services-plus:9.0.2'
compile 'com.google.android.gms:play-services-ads:9.0.2'
compile 'com.google.android.gms:play-services-gcm:9.0.2'
compile 'com.google.android.gms:play-services-maps:9.0.2'
compile 'com.google.android.gms:play-services-auth:9.0.2'
compile 'com.google.android.gms:play-services-base:9.0.2'
compile 'com.google.android.gms:play-services-identity:9.0.2'
compile 'com.google.api-client:google-api-client:1.23.0'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1' …Run Code Online (Sandbox Code Playgroud) google-app-engine android google-api-java-client google-play-services build.gradle
如何在谷歌驱动器“appDataFolder”中创建文件?
我创建了谷歌开发者控制台设置,这是创建文件和访问“appDataFolder”所必需的。
Android Studio 输出错误消息如下。
W/System.err: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code": 403,
"errors": [
{
"domain": "global",
"message": "The granted scopes do not allow use of the Application Data folder.",
"reason": "insufficientScopes"
}
],
"message": "The granted scopes do not allow use of the Application Data folder."
}
W/System.err: at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:432)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at com.example.test1.MainActivity$3.run(MainActivity.java:131)
at java.lang.Thread.run(Thread.java:923)
Run Code Online (Sandbox Code Playgroud)
源代码是
File body = new File();
body.setName(FILE_TITLE);//fileContent.getName());
body.setMimeType("text/plain");
body.setParents(Collections.singletonList("appDataFolder"));
File file = service.files().create(body, content)
.setFields("id")
.execute(); …Run Code Online (Sandbox Code Playgroud) java google-api-java-client google-drive-api google-oauth android-studio
我正在尝试官方示例calendar-v2-atom-android.我已经使用maven导入所有依赖项并且它已成功编译.但是,在运行时,没有发现类def def错误
private final List calendars = Lists.newArrayList();
private final HttpTransport transport = AndroidHttp.newCompatibleTransport();
它似乎无法在运行时中找到库中的类.
我应该在日食中做些什么?
谢谢.
我正在使用相同的查询以两种不同的方式获取Calendar事件.奇怪的是,它的一种工作方式,另一种方式,即使一切都几乎完全相同.使用timeMin,Google API Explorer上的timeMax会在第二种方法发送时返回错误请求.
不仅如此,Google Calendar API文档说该字段应该是start-min和start-max,而客户端和API Explorer说该字段应该是timeMin和timeMax.
以下是方法1的代码:
void setPeriod() {
Date nowDate = new Date();
startTime = new DateTime(nowDate);
Time t = new Time();
t.setToNow();
t.hour = 23;
t.minute = 59;
nowDate.setTime(t.toMillis(false));
endTime = new DateTime(nowDate);
}
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
Events evlist = g.getClient().events().list(calendarIds.get(cal)).setTimeMin(startTime).setTimeMax(endTime).execute();
if (evlist.getItems() != null) {
//Parse the events
...
}.execute();
Run Code Online (Sandbox Code Playgroud)
方法2:
new AsyncTask<Void, Void, List<Event>>() {
@Override
protected List<Event> doInBackground(Void... arg0) {
Date nowDate = …Run Code Online (Sandbox Code Playgroud) 文档对此有点不清楚.我真的可以这样做吗?到目前为止,我见过的唯一示例来自Google文档,该文档使用GoogleAuthorizationCodeFlow类来获取授权.我已经看到了一些使用服务帐户更新和检索用户列表的示例,但没有任何内容可供创建.这可能吗?如果是这样,有没有人知道任何漂浮的例子?我似乎无法找到解决它的任何东西.
已经使用 API 一段时间了,但遇到了一个问题:“youtube.part”上的 400 错误请求。通常不是问题,我想也许我指定了一个不正确的零件,错误消息会告诉我什么是错误的,我可以轻松地更改它,但是,错误如下:
400 Bad Request
{
"code" : 400,
"errors" : [ {
"domain" : "youtube.part",
"location" : "part",
"locationType" : "parameter",
"message" : "{0}",
"reason" : "unexpectedPart"
} ],
"message" : "{0}"
}
Run Code Online (Sandbox Code Playgroud)
通常在"message": "{0}"它应该告诉你哪个部分是错误的。在这里搜索了一下,我似乎找不到解决方案,现在我只指定snippet. 在构建我的更新请求时,我只指定snippet.title snippet.tags[]& snippet.categoryId。
上述内容用于测试环境,并且请求已获得youtube范围授权,因为我使用它将视频上传到同一 youtube 频道。
我自己的想法可能是:
加载后无法直接更新视频(但是,可以列出)
我不知何故遇到了一个错误
对于那些希望重现我所得到的东西的人,我在测试中采取了以下步骤:
使用授权凭据设置 youtube 实例
上传一个随机的小文件到频道(设置视频标题等)
成功返回后尝试按 id 列出视频
使用此 ID,从 youtube 检索视频资源
更改标签部分,保持标题等不变
尝试更新并收到该错误
任何帮助将不胜感激,我真的希望这不是一个明显的解决方案:)