gpt4 book ai didi

android - GoogleAuthException 未知来源

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:27:08 25 4
gpt4 key购买 nike

我正在使用 GoogleUtilAuth.getToken() 生成 token 。我还生成了两个客户端 ID,一个用于网页,一个用于 Android 应用程序,两者都在同一个项目中。点击此链接

verification from back end server

这是我的代码:

package com.example.tokengenerate;

import java.io.IOException;

import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.auth.UserRecoverableAuthException;
import com.google.android.gms.common.Scopes;

import android.os.AsyncTask;
import android.os.Bundle;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView tv;
String scope="audience:server:client_id:CLIENTID OF WEBPAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv =(TextView)findViewById(R.id.printId);
new AsyncTask<Void, Void, Void>() {
String id=null;
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
tv.setText(id);
super.onPostExecute(result);
}

@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub

String[] account=getAccountNames();
for(int i =0;i<account.length;i++)
{
try {
Log.e("account name", account[i]);
id=GoogleAuthUtil.getToken(MainActivity.this, account[i], scope);
Log.e("google id",id);
} catch (UserRecoverableAuthException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (GoogleAuthException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}



return null;
}
}.execute(null,null,null);


}
private String[] getAccountNames() {
AccountManager mAccountManager = AccountManager.get(this);
Account[] accounts = mAccountManager.getAccountsByType(
GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
String[] names = new String[accounts.length];
for (int i = 0; i < names.length; i++) {
names[i] = accounts[i].name;
}
return names;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

这是我的 Logcat:

08-27 19:19:54.641: W/System.err(9792): com.google.android.gms.auth.GoogleAuthException: Unknown
08-27 19:19:54.651: W/System.err(9792): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
08-27 19:19:54.651: W/System.err(9792): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
08-27 19:19:54.651: W/System.err(9792): at com.example.tokengenerate.MainActivity$1.doInBackground(MainActivity.java:45)
08-27 19:19:54.661: W/System.err(9792): at com.example.tokengenerate.MainActivity$1.doInBackground(MainActivity.java:1)
08-27 19:19:54.661: W/System.err(9792): at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-27 19:19:54.661: W/System.err(9792): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-27 19:19:54.661: W/System.err(9792): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-27 19:19:54.661: W/System.err(9792): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-27 19:19:54.661: W/System.err(9792): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-27 19:19:54.661: W/System.err(9792): at java.lang.Thread.run(Thread.java:864)

完全卡住了,请帮忙。没明白我要去的地方

最佳答案

使用此代码获取范围

String scope = "oauth2:" + Scopes.PROFILE;

并在这里使用这个scpe

id=GoogleAuthUtil.getToken(MainActivity.this, account[i], scope);

然后你可以在变量“id”中获取你的访问 token 。简单方法。

关于android - GoogleAuthException 未知来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18467847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com